29 #ifndef FASTCGIPP_HTTP_HPP 30 #define FASTCGIPP_HTTP_HPP 65 template<
class charT>
struct File 77 mutable std::unique_ptr<char[]>
data;
107 template<
class charT,
class Traits>
109 std::basic_ostream<charT, Traits>& os,
112 return os << requestMethodLabels[static_cast<int>(requestMethod)];
188 template<
class charT>
void assign(
206 operator bool()
const;
224 template<
class charT,
class Traits>
225 std::basic_ostream<charT, Traits>&
operator<<(
226 std::basic_ostream<charT, Traits>& os,
227 const Address& address);
235 template<
class charT,
class Traits>
236 std::basic_istream<charT, Traits>&
operator>>(
237 std::basic_istream<charT, Traits>& is,
318 std::basic_string<charT>,
323 std::basic_string<charT>,
324 std::basic_string<charT>>
gets;
328 std::basic_string<charT>,
333 std::basic_string<charT>,
348 const char* dataEnd);
417 std::wstring&
string);
430 string.assign(start, end);
446 template<
class charT>
int atoi(
const charT* start,
const charT* end);
461 template<
class charT>
float atof(
const charT* start,
const charT* end);
474 std::basic_string<charT>,
475 std::basic_string<charT>>& output,
476 const char*
const fieldSeparator=
"&");
518 template<
class In,
class Out>
538 template<
class In,
class Out>
586 template<
class charT>
587 SessionId(
const std::basic_string<charT>&
string);
589 template<
class charT,
class Traits>
590 friend std::basic_ostream<charT, Traits>&
operator<<(
591 std::basic_ostream<charT, Traits>& os,
612 template<
class charT,
class Traits>
614 std::basic_ostream<charT, Traits>& os,
620 std::ostream_iterator<charT, charT, Traits>(os));
687 std::shared_ptr<const T>
get(
const SessionId& id);
692 std::lock_guard<std::mutex> lock(
m_mutex);
711 std::lock_guard<std::mutex> lock(
m_mutex);
724 template<
class In,
class Out>
727 Out dest=destination;
729 for(
int buffer, bitPos=-8, padStart; start!=end || bitPos>-6; ++dest)
738 if(start==end)
return destination;
740 if(value >=
'A' &&
'Z' >= value) value -=
'A';
741 else if(value >=
'a' &&
'z' >= value) value -=
'a' - 26;
742 else if(value >=
'0' &&
'9' >= value) value -=
'0' - 52;
743 else if(value ==
'+') value = 62;
744 else if(value ==
'/') value = 63;
745 else if(value ==
'=') { padStart=bitPos;
break; }
746 else return destination;
748 buffer |= value << bitPos;
754 *dest = (buffer >> bitPos) & 0xff;
758 if( (padStart-bitPos)/6 )
768 template<
class In,
class Out>
771 for(
int buffer, bitPos=-6, padded; start!=end || bitPos>-6; ++destination)
781 buffer |=
static_cast<int>(
782 *
reinterpret_cast<const unsigned char*
>(
805 std::lock_guard<std::mutex> lock(m_mutex);
807 typename std::map<SessionId, std::shared_ptr<const T>>::iterator,
811 while(!retVal.second)
812 retVal=m_sessions.insert(std::pair<
SessionId, std::shared_ptr<const T>>(
815 return retVal.first->first;
818 template<
class T> std::shared_ptr<const T>
821 std::lock_guard<std::mutex> lock(m_mutex);
822 const std::time_t now = std::time(
nullptr);
823 const std::time_t oldest(now-m_keepAlive);
825 if(now >= m_cleanupTime)
827 auto session = m_sessions.begin();
828 while(session != m_sessions.end())
830 if(session->first.m_timestamp < oldest)
831 session = m_sessions.erase(session);
835 m_cleanupTime = std::time(
nullptr)+m_keepAlive;
839 const auto session = m_sessions.find(
id);
840 if(session != m_sessions.cend())
842 if(session->first.m_timestamp < oldest)
843 m_sessions.erase(session);
846 session->first.refresh();
847 return session->second;
851 return std::shared_ptr<const T>();
856 char*
const newExpiration(
857 m_expirationPtr==m_expiration[0]?m_expiration[1]:m_expiration[0]);
858 const std::time_t expirationTime = m_cleanupTime + m_keepAlive;
859 const auto count = std::strftime(
862 "%a, %d %b %Y %H:%M:%S GMT",
863 std::gmtime(&expirationTime));
864 std::fill(newExpiration+count, newExpiration+expirationLength, 0);
865 m_expirationPtr = newExpiration;
float atof(const charT *start, const charT *end)
Convert a char string to a float.
std::array< unsigned char, size > m_data
Data representation of the IPv6 address.
Topmost namespace for the fastcgi++ library.
RequestMethod
The HTTP request method as an enumeration.
std::vector< char > m_postBuffer
Buffer for processing post data.
uint16_t remotePort
TCP port used by the client.
std::basic_string< charT > filename
Filename.
Address operator &(const Address &x) const
unsigned etag
The etag the client assumes this document should have.
void assign(const charT *start, const charT *end)
Assign the IP address from a string of characters.
std::basic_istream< charT, Traits > & operator>>(std::basic_istream< charT, Traits > &is, Address &address)
Address stream extractor operation.
void refresh() const
Resets the last access timestamp to the current time.
std::multimap< std::basic_string< charT >, File< charT > > files
Container of file POST data.
bool parsePostBuffer()
Attempts to parse the POST buffer.
std::basic_string< charT > host
Hostname of the server.
bool operator<(const Address &x) const
std::atomic< const char * > m_expirationPtr
Point for the current expiration string.
void fillPostBuffer(const char *start, const char *end)
Consolidates POST data into a single buffer.
Holds a file uploaded from the client.
size_t size() const
How many active sessions are there?
std::shared_ptr< const T > get(const SessionId &id)
Get session data from session ID.
std::mutex m_mutex
Thread safe all operations.
void parsePostsUrlEncoded()
Parses "application/x-www-form-urlencoded" post data.
Address serverAddress
IP address of the server.
std::basic_string< charT > root
HTTP root directory.
std::multimap< std::basic_string< charT >, std::basic_string< charT > > gets
Container with all url-encoded GET data.
std::time_t m_timestamp
Contains the time this session was last used.
std::time_t ifModifiedSince
Timestamp the client has for this document.
std::basic_string< charT > requestUri
REQUEST_URI.
char * percentEscapedToRealBytes(const char *start, const char *end, char *destination)
Convert a string with percent escaped byte values to their values.
std::vector< char > boundary
Raw string of characters representing the post boundary.
File(File &&x)
Move constructor.
friend std::basic_ostream< charT, Traits > & operator<<(std::basic_ostream< charT, Traits > &os, const SessionId &x)
Output the ID data in base64 encoding.
std::array< unsigned char, size > m_data
ID data.
Declares everything for relating to the FastCGI protocol itself.
const std::array< const char *const, 9 > requestMethodLabels
Some textual labels for RequestMethod.
void vecToString(const char *start, const char *end, std::wstring &string)
Convert a char array to a std::wstring.
Sessions(unsigned int keepAlive)
Constructor takes session keep alive times.
std::basic_string< charT > contentType
Content type of data sent from client.
std::unique_ptr< char[]> data
File data.
void zero()
Set all bits to zero in IP address.
Defines ID values for HTTP sessions.
std::mutex mutex
Thread safe the logging mechanism.
unsigned contentLength
Length of content to be received from the client (post data)
int atoi(const charT *start, const charT *end)
Convert a char string to an integer.
void decodeUrlEncoded(const char *data, const char *dataEnd, std::multimap< std::basic_string< charT >, std::basic_string< charT >> &output, const char *const fieldSeparator="&")
Decodes a url-encoded string into a multimap container.
bool operator==(const Address &x) const
uint16_t serverPort
TCP port used by the server.
std::basic_string< charT > authorization
Http authorization string.
Data structure of HTTP environment data.
std::vector< std::basic_string< charT > > pathInfo
Path information.
std::multimap< std::basic_string< charT >, std::basic_string< charT > > posts
Container of none-file POST data.
std::basic_string< charT > userAgent
User agent string.
SessionId generate(const std::shared_ptr< const T > &data)
Generates a new session.
Out base64Decode(In start, In end, Out destination)
Convert a Base64 encoded container to a binary container.
void setExpiration()
Internal helper for building the m_expiration string.
Address operator=(const unsigned char *data)
Assign the IPv6 address from a data array.
void clearPostBuffer()
Clear the post buffer.
static const size_t expirationLength
Length of expiration string (with null terminator)
const unsigned int m_keepAlive
Amount of seconds to keep sessions around for.
void fill(const char *data, const char *dataEnd)
Parses FastCGI parameter data into the data structure.
std::time_t m_cleanupTime
The time that the next session cleanup should be done.
static const size_t stringLength
Size in characters of string representation.
std::basic_string< charT > acceptCharsets
Character sets the clients accepts.
Address operator=(const Address &address)
const char * expiration() const
Expiration string for setting cookies.
std::multimap< std::basic_string< charT >, std::basic_string< charT > > cookies
Container with all url-encoded cookie data.
static const size_t size
Size in bytes of the ID data. Make sure it is a multiple of 3.
std::map< SessionId, std::shared_ptr< const T > > m_sessions
Actual container of sessions.
SessionId(const SessionId &x)
std::basic_ostream< charT, Traits > & operator<<(std::basic_ostream< charT, Traits > &os, const RequestMethod requestMethod)
bool operator<(const SessionId &x) const
std::basic_string< charT > referer
Referral URL.
static const size_t size
This is the data length of the IPv6 address.
std::basic_string< charT > contentType
Content Type.
void parsePostsMultipart()
Parses "multipart/form-data" http post data.
Address & operator &=(const Address &x)
std::basic_string< charT > acceptContentTypes
Content types the client accepts.
std::basic_string< charT > scriptName
Filename of script relative to the HTTP root directory.
unsigned keepAlive
How many seconds the connection should be kept alive.
Address(const unsigned char *data)
Construct the IPv6 address from a data array.
const std::vector< char > & postBuffer() const
Get the post buffer.
const std::array< const char, 64 > base64Characters
List of characters in order for Base64 encoding.
Container for HTTP sessions.
char m_expiration[2][expirationLength]
Internal string for cookie expirations.
Efficiently stores IPv6 addresses.
void erase(const SessionId &id)
Erase a session.
Out base64Encode(In start, In end, Out destination)
Convert a binary container of data to a Base64 encoded container.
SessionId()
This constructor initializes the ID data to a random value.
Address()
Initializes an all zero address.
std::vector< std::string > acceptLanguages
Languages the client accepts.
Address remoteAddress
IP address of the client.
bool operator==(const SessionId &x) const
Address(const Address &address)
RequestMethod requestMethod
REQUEST_METHOD.