PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/ahttplib/ahttp/http_support.hpp

http://ahttpserver.codeplex.com
C++ Header | 480 lines | 398 code | 42 blank | 40 comment | 0 complexity | 0caf5a0ab1045864c9d1d4a1b02418fc MD5 | raw file
  1. /*
  2. This file is part of [ahttp] library.
  3. Author: Artem Kustikov (kustikoff[at]tut.by)
  4. This code is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this code.
  7. Permission is granted to anyone to use this code for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this code must not be misrepresented; you must
  11. not claim that you wrote the original code. If you use this
  12. code in a product, an acknowledgment in the product documentation
  13. would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original code.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #ifndef AHTTP_HTTP_SUPPORT_H
  20. #define AHTTP_HTTP_SUPPORT_H
  21. #include <vector>
  22. #include <boost/cstdint.hpp>
  23. #include "aconnect/util.hpp"
  24. #include "aconnect/types.hpp"
  25. #include "aconnect/logger.hpp"
  26. #include "aconnect/istoppable.hpp"
  27. #include "ahttp/aconnect_types.hpp"
  28. namespace ahttp
  29. {
  30. namespace port
  31. {
  32. const aconnect::port_type ftp = 21;
  33. const aconnect::port_type http = 80;
  34. const aconnect::port_type https = 443;
  35. };
  36. namespace HttpMethod
  37. {
  38. enum HttpMethodType
  39. {
  40. Unknown = 0,
  41. Get = 1,
  42. Post = 2,
  43. Head = 3,
  44. };
  45. };
  46. enum WebDirectoryItemType // defines sorting order
  47. {
  48. WdUnknown,
  49. WdVirtualDirectory,
  50. WdDirectory,
  51. WdFile,
  52. };
  53. enum WebDirectorySortType
  54. {
  55. WdSortByName,
  56. WdSortByTypeAndName,
  57. };
  58. struct WebDirectoryItem
  59. {
  60. WebDirectoryItemType type;
  61. string name;
  62. string url;
  63. boost::uintmax_t size;
  64. std::time_t lastWriteTime;
  65. WebDirectoryItem () : type (WdUnknown), size(-1), lastWriteTime(-1) { }
  66. inline void clear()
  67. {
  68. type = WdUnknown;
  69. name.clear();
  70. url.clear();
  71. size = -1;
  72. lastWriteTime = -1;
  73. }
  74. };
  75. namespace HttpStatus
  76. {
  77. // HTTP Statuses
  78. enum HttpStatusType
  79. {
  80. Continue = 100,
  81. SwitchingProtocols = 101,
  82. OK = 200,
  83. Created = 201,
  84. Accepted = 202,
  85. NonAuthoritativeInformation = 203,
  86. NoContent = 204,
  87. ResetContent = 205,
  88. PartialContent = 206,
  89. MultipleChoices = 300,
  90. MovedPermanently = 301,
  91. Found = 302,
  92. SeeOther = 303,
  93. NotModified = 304,
  94. UseProxy = 305,
  95. Unused = 306,
  96. TemporaryRedirect = 307,
  97. BadRequest = 400,
  98. Unauthorized = 401,
  99. PaymentRequired = 402,
  100. Forbidden = 403,
  101. NotFound = 404,
  102. MethodNotAllowed = 405,
  103. NotAcceptable = 406,
  104. ProxyAuthenticationRequired = 407,
  105. RequestTimeout = 408,
  106. Conflict = 409,
  107. Gone = 410,
  108. LengthRequired = 411,
  109. PreconditionFailed = 412,
  110. RequestEntityTooLarge = 413,
  111. RequestUriTooLong = 414,
  112. UnsupportedMediaType = 415,
  113. RequestedRangeNotSatisfiable = 416,
  114. ExpectationFailed = 417,
  115. InternalServerError = 500,
  116. NotImplemented = 501,
  117. BadGateway = 502,
  118. ServiceUnavailable = 503,
  119. GatewayTimeout = 504,
  120. HttpVersionNotSupported = 505
  121. };
  122. }
  123. namespace ContentEncoding
  124. {
  125. enum ContentEncodingType
  126. {
  127. Identity = 0,
  128. Gzip = 1,
  129. Compress = 2,
  130. Deflate = 3,
  131. };
  132. };
  133. namespace strings
  134. {
  135. using namespace aconnect;
  136. const char_type WeekDays_RFC1123[7][4] = {
  137. "Sun", "Mon", "Tue", "Wed", "Thu" , "Fri" , "Sat"
  138. };
  139. const char_type Months_RFC1123[12][4] = {
  140. "Jan" , "Feb" , "Mar" , "Apr",
  141. "May" , "Jun" , "Jul" , "Aug",
  142. "Sep" , "Oct" , "Nov" , "Dec"
  143. };
  144. // HTTP Methods
  145. string_constant HttpMethodGet = "GET";
  146. string_constant HttpMethodPost = "POST";
  147. string_constant HttpMethodHead = "HEAD";
  148. // HTTP Headers
  149. string_constant HeaderAccept = "Accept";
  150. string_constant HeaderAcceptCharset = "Accept-Charset";
  151. string_constant HeaderAcceptEncoding = "Accept-Encoding";
  152. string_constant HeaderAcceptLanguage = "Accept-Language";
  153. string_constant HeaderAcceptRanges = "Accept-Ranges";
  154. string_constant HeaderAge = "Age";
  155. string_constant HeaderAllow = "Allow";
  156. string_constant HeaderAuthorization = "Authorization";
  157. string_constant HeaderCacheControl = "Cache-Control";
  158. string_constant HeaderConnection = "Connection";
  159. string_constant HeaderContentEncoding = "Content-Encoding";
  160. string_constant HeaderContentDisposition = "Content-Disposition";
  161. string_constant HeaderContentLanguage = "Content-Language";
  162. string_constant HeaderContentLength = "Content-Length";
  163. string_constant HeaderContentLocation = "Content-Location";
  164. string_constant HeaderContentMD5 = "Content-MD5";
  165. string_constant HeaderContentRange = "Content-Range";
  166. string_constant HeaderContentType = "Content-Type";
  167. string_constant HeaderCookie = "Cookie";
  168. string_constant HeaderDate = "Date";
  169. string_constant HeaderETag = "ETag";
  170. string_constant HeaderExpect = "Expect";
  171. string_constant HeaderExpires = "Expires";
  172. string_constant HeaderFrom = "From";
  173. string_constant HeaderHost = "Host";
  174. string_constant HeaderIfMatch = "If-Match";
  175. string_constant HeaderIfModifiedSince = "If-Modified-Since";
  176. string_constant HeaderIfNoneMatch = "If-None-Match";
  177. string_constant HeaderIfRange = "If-Range";
  178. string_constant HeaderIfUnmodifiedSince = "If-Unmodified-Since";
  179. string_constant HeaderKeepAlive = "Keep-Alive";
  180. string_constant HeaderLastModified = "Last-Modified";
  181. string_constant HeaderLocation = "Location";
  182. string_constant HeaderMaxForwards = "Max-Forwards";
  183. string_constant HeaderPragma = "Pragma";
  184. string_constant HeaderProxyAuthenticate = "Proxy-Authenticate";
  185. string_constant HeaderProxyAuthorization = "Proxy-Authorization";
  186. string_constant HeaderProxyConnection = "Proxy-Connection";
  187. string_constant HeaderRange = "Range";
  188. string_constant HeaderReferer = "Referer";
  189. string_constant HeaderRetryAfter = "Retry-After";
  190. string_constant HeaderServer = "Server";
  191. string_constant HeaderStatus = "Status";
  192. string_constant HeaderTE = "TE";
  193. string_constant HeaderTrailer = "Trailer";
  194. string_constant HeaderTransferEncoding = "Transfer-Encoding";
  195. string_constant HeaderUpgrade = "Upgrade";
  196. string_constant HeaderUserAgent = "User-Agent";
  197. string_constant HeaderVary = "Vary";
  198. string_constant HeaderVia = "Via";
  199. string_constant HeaderWarning = "Warning";
  200. string_constant HeaderWWWAuthenticate = "WWW-Authenticate";
  201. // HTTP headers values
  202. string_constant ConnectionKeepAlive = "Keep-Alive";
  203. string_constant ConnectionClose = "Close";
  204. string_constant ContentTypeTextHtml = "text/html";
  205. string_constant ContentTypeOctetStream = "application/octet-stream";
  206. string_constant ContentTypeMultipartFormData = "multipart/form-data";
  207. string_constant ContentDispositionFormData = "form-data";
  208. string_constant ContentDispositionAttachment = "attachment";
  209. string_constant TransferEncodingChunked = "chunked";
  210. string_constant CacheControlNoCache = "no-cache";
  211. string_constant CacheControlPrivate = "private";
  212. string_constant HttpsDisabledValue = "off";
  213. string_constant AcceptRangesBytes = "bytes";
  214. //==================================================================//
  215. // Content encoding support //
  216. //==================================================================//
  217. string_constant ContentEncodingGzip = "gzip";
  218. string_constant ContentEncodingGzipX = "x-gzip";
  219. string_constant ContentEncodingCompress = "compress";
  220. string_constant ContentEncodingCompressX = "x-compress";
  221. string_constant ContentEncodingDeflate = "deflate";
  222. string_constant ContentEncodingIdentity = "identity";
  223. //////////////////////////////////////////////////////////////////////////
  224. //
  225. // Server variables
  226. //
  227. namespace ServerVariables
  228. {
  229. string_constant ALL_RAW = "ALL_RAW";
  230. string_constant APPL_MD_PATH = "APPL_MD_PATH";
  231. string_constant APPL_PHYSICAL_PATH = "APPL_PHYSICAL_PATH";
  232. string_constant APPL_PATH = "APPL_PATH";
  233. string_constant AUTH_PASSWORD = "AUTH_PASSWORD";
  234. string_constant AUTH_TYPE = "AUTH_TYPE";
  235. string_constant AUTH_USER = "AUTH_USER";
  236. string_constant CERT_COOKIE = "CERT_COOKIE";
  237. string_constant CERT_FLAGS = "CERT_FLAGS";
  238. string_constant CERT_ISSUER = "CERT_ISSUER";
  239. string_constant CERT_KEYSIZE = "CERT_KEYSIZE";
  240. string_constant CERT_SECRETKEYSIZE = "CERT_SECRETKEYSIZE";
  241. string_constant CERT_SERIALNUMBER = "CERT_SERIALNUMBER";
  242. string_constant CERT_SERVER_ISSUER = "CERT_SERVER_ISSUER";
  243. string_constant CERT_SERVER_SUBJECT = "CERT_SERVER_SUBJECT";
  244. string_constant CERT_SUBJECT = "CERT_SUBJECT";
  245. string_constant CONTENT_LENGTH = "CONTENT_LENGTH";
  246. string_constant CONTENT_TYPE = "CONTENT_TYPE";
  247. string_constant DOCUMENT_ROOT = "DOCUMENT_ROOT";
  248. string_constant GATEWAY_INTERFACE = "GATEWAY_INTERFACE";
  249. string_constant HTTPS = "HTTPS";
  250. string_constant HTTPS_KEYSIZE = "HTTPS_KEYSIZE";
  251. string_constant HTTPS_SECRETKEYSIZE = "HTTPS_SECRETKEYSIZE";
  252. string_constant HTTPS_SERVER_ISSUER = "HTTPS_SERVER_ISSUER";
  253. string_constant HTTPS_SERVER_SUBJECT = "HTTPS_SERVER_SUBJECT";
  254. string_constant INSTANCE_ID = "INSTANCE_ID";
  255. string_constant INSTANCE_META_PATH = "INSTANCE_META_PATH";
  256. string_constant LOCAL_ADDR = "LOCAL_ADDR";
  257. string_constant LOGON_USER = "LOGON_USER";
  258. string_constant PATH_INFO = "PATH_INFO";
  259. string_constant PATH_TRANSLATED = "PATH_TRANSLATED";
  260. string_constant QUERY_STRING = "QUERY_STRING";
  261. string_constant REMOTE_ADDR = "REMOTE_ADDR";
  262. string_constant REMOTE_HOST = "REMOTE_HOST";
  263. string_constant REMOTE_USER = "REMOTE_USER";
  264. string_constant REQUEST_METHOD = "REQUEST_METHOD";
  265. string_constant REQUEST_URI = "REQUEST_URI";
  266. string_constant SCRIPT_NAME = "SCRIPT_NAME";
  267. string_constant SCRIPT_FILENAME = "SCRIPT_FILENAME";
  268. string_constant SERVER_NAME = "SERVER_NAME";
  269. string_constant SERVER_ADDR = "SERVER_ADDR";
  270. string_constant SERVER_PORT = "SERVER_PORT";
  271. string_constant SERVER_PORT_SECURE = "SERVER_PORT_SECURE";
  272. string_constant SERVER_PROTOCOL = "SERVER_PROTOCOL";
  273. string_constant SERVER_SOFTWARE = "SERVER_SOFTWARE";
  274. string_constant URL = "URL";
  275. string_constant URL_ENCODED = "URL_ENCODED";
  276. string_constant HTTP_ACCEPT = "HTTP_ACCEPT";
  277. string_constant HTTP_ACCEPT_LANGUAGE = "HTTP_ACCEPT_LANGUAGE";
  278. string_constant HTTP_COOKIE = "HTTP_COOKIE";
  279. string_constant HTTP_CONNECTION = "HTTP_CONNECTION";
  280. string_constant HTTP_HOST = "HTTP_HOST";
  281. string_constant HTTP_REFERER = "HTTP_REFERER";
  282. string_constant HTTP_USER_AGENT = "HTTP_USER_AGENT";
  283. string_constant HTTP_ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING";
  284. string_constant HTTP_ACCEPT_CHARSET = "HTTP_ACCEPT_CHARSET";
  285. string_constant HTTP_KEEP_ALIVE = "HTTP_KEEP_ALIVE";
  286. extern const char_type* AllVariablesList[];
  287. extern const int AllVariablesCount;
  288. extern const char_type* CgiVariablesList[];
  289. extern const int CgiVariablesCount;
  290. }
  291. //////////////////////////////////////////////////////////////////////////
  292. //
  293. // Common server definitions
  294. //
  295. string_constant Slash = "/";
  296. string_constant WindowsSlash = "\\";
  297. const char_type SlashCh = '/';
  298. string_constant ChunkHeaderFormat = "%x\r\n";
  299. string_constant ChunkEndMark = "\r\n";
  300. string_constant LastChunkFormat = "0\r\n\r\n";
  301. string_constant HttpVersion = "HTTP/1.1";
  302. string_constant HeadersDelimiter = "\r\n";
  303. string_constant HeadersEndMark = "\r\n\r\n";
  304. string_constant HeaderValueDelimiter = ": ";
  305. string_constant DefaultContentCharset = "ISO-8859-1"; // q=1 by default: HTTP RFC
  306. string_constant ContentCharsetUtf8 = "UTF-8";
  307. string_constant AnyContentCharsetMark = "*";
  308. string_constant MultipartBoundaryMark = "boundary=";
  309. string_constant MultipartBoundaryPrefix = "--";
  310. string_constant GatewayInterfaceCgi1_1 = "CGI/1.1";
  311. //
  312. //////////////////////////////////////////////////////////////////////////
  313. inline string httpMethodName (int status)
  314. {
  315. switch (status)
  316. {
  317. case HttpMethod::Get: return HttpMethodGet;
  318. case HttpMethod::Post: return HttpMethodPost;
  319. case HttpMethod::Head: return HttpMethodHead;
  320. default:
  321. return "";
  322. };
  323. }
  324. inline string_constptr contentEncodingName (ContentEncoding::ContentEncodingType encoding)
  325. {
  326. switch (encoding)
  327. {
  328. case ContentEncoding::Gzip:
  329. return ContentEncodingGzip;
  330. case ContentEncoding::Deflate:
  331. return ContentEncodingDeflate;
  332. case ContentEncoding::Compress:
  333. return ContentEncodingCompress;
  334. default:
  335. return NULL;
  336. };
  337. }
  338. inline string httpStatusDesc (int status)
  339. {
  340. string desc;
  341. switch(status)
  342. {
  343. case 100 : desc = ("Continue"); break;
  344. case 101 : desc = ("Switching Protocols"); break;
  345. case 200 : desc = ("OK"); break;
  346. case 201 : desc = ("Created"); break;
  347. case 202 : desc = ("Accepted"); break;
  348. case 203 : desc = ("Non-Authoritative Information"); break;
  349. case 204 : desc = ("No Content"); break;
  350. case 205 : desc = ("Reset Content"); break;
  351. case 206 : desc = ("Partial Content"); break;
  352. case 300 : desc = ("Multiple Choices"); break;
  353. case 301 : desc = ("Moved Permanently"); break;
  354. case 302 : desc = ("Found"); break;
  355. case 303 : desc = ("See Other"); break;
  356. case 304 : desc = ("Not Modified"); break;
  357. case 305 : desc = ("Use Proxy"); break;
  358. case 306 : desc = ("(Unused)"); break;
  359. case 307 : desc = ("Temporary Redirect"); break;
  360. case 400 : desc = ("Bad Request"); break;
  361. case 401 : desc = ("Unauthorized"); break;
  362. case 402 : desc = ("Payment Required"); break;
  363. case 403 : desc = ("Forbidden"); break;
  364. case 404 : desc = ("Not Found"); break;
  365. case 405 : desc = ("Method Not Allowed"); break;
  366. case 406 : desc = ("Not Acceptable"); break;
  367. case 407 : desc = ("Proxy Authentication Required"); break;
  368. case 408 : desc = ("Request Timeout"); break;
  369. case 409 : desc = ("Conflict"); break;
  370. case 410 : desc = ("Gone"); break;
  371. case 411 : desc = ("Length Required"); break;
  372. case 412 : desc = ("Precondition Failed"); break;
  373. case 413 : desc = ("Request Entity Too Large"); break;
  374. case 414 : desc = ("Request-URI Too Long"); break;
  375. case 415 : desc = ("Unsupported Media Type"); break;
  376. case 416 : desc = ("Requested Range Not Satisfiable"); break;
  377. case 417 : desc = ("Expectation Failed"); break;
  378. case 500 : desc = ("Internal Server Error"); break;
  379. case 501 : desc = ("Not Implemented"); break;
  380. case 502 : desc = ("Bad Gateway"); break;
  381. case 503 : desc = ("Service Unavailable"); break;
  382. case 504 : desc = ("Gateway Timeout"); break;
  383. case 505 : desc = ("HTTP Version Not Supported"); break;
  384. default:
  385. desc = ("Undefined");
  386. }
  387. return desc;
  388. };
  389. }
  390. //////////////////////////////////////////////////////////////////////////
  391. //
  392. // Support functions
  393. void readDirectoryContent (string_constref dirPath,
  394. string_constref dirVirtPath,
  395. std::vector<WebDirectoryItem> &items,
  396. class aconnect::Logger& logger,
  397. size_t &errCount,
  398. aconnect::IStoppable *stopable,
  399. bool showHiddenFiles = false,
  400. WebDirectorySortType sortType = WdSortByName);
  401. // sample: Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
  402. string formatDate_RFC1123 (const struct tm& dateTime);
  403. // sample: Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
  404. std::time_t getDateFrom_RFC1123 (string_constref date);
  405. }
  406. #endif // AHTTP_HTTP_SUPPORT_H