HTTP SERVER

终彬郁
2023-12-01
#ifndef HTTP_CORE_H#define HTTP_CORE_H#include #include #include #include #include #include #include #include using namespace std;#define OK 0#define FAIL -1#define RESPONSE_CODES 57#define HTTP_CONTINUE 100#define HTTP_SWITCHING_PROTOCOLS 101#define HTTP_PROCESSING 102#define HTTP_OK 200#define HTTP_CREATED 201#define HTTP_ACCEPTED 202#define HTTP_NON_AUTHORITATIVE 203#define HTTP_NO_CONTENT 204#define HTTP_RESET_CONTENT 205#define HTTP_PARTIAL_CONTENT 206#define HTTP_MULTI_STATUS 207#define HTTP_MULTIPLE_CHOICES 300#define HTTP_MOVED_PERMANENTLY 301#define HTTP_MOVED_TEMPORARILY 302#define HTTP_SEE_OTHER 303#define HTTP_NOT_MODIFIED 304#define HTTP_USE_PROXY 305#define HTTP_TEMPORARY_REDIRECT 307#define HTTP_BAD_REQUEST 400#define HTTP_UNAUTHORIZED 401#define HTTP_PAYMENT_REQUIRED 402#define HTTP_FORBIDDEN 403#define HTTP_NOT_FOUND 404#define HTTP_METHOD_NOT_ALLOWED 405#define HTTP_NOT_ACCEPTABLE 406#define HTTP_PROXY_AUTHENTICATION_REQUIRED 407#define HTTP_REQUEST_TIME_OUT 408#define HTTP_CONFLICT 409#define HTTP_GONE 410#define HTTP_LENGTH_REQUIRED 411#define HTTP_PRECONDITION_FAILED 412#define HTTP_REQUEST_ENTITY_TOO_LARGE 413#define HTTP_REQUEST_URI_TOO_LARGE 414#define HTTP_UNSUPPORTED_MEDIA_TYPE 415#define HTTP_RANGE_NOT_SATISFIABLE 416#define HTTP_EXPECTATION_FAILED 417#define HTTP_UNPROCESSABLE_ENTITY 422#define HTTP_LOCKED 423#define HTTP_FAILED_DEPENDENCY 424#define HTTP_UPGRADE_REQUIRED 426#define HTTP_INTERNAL_SERVER_ERROR 500#define HTTP_NOT_IMPLEMENTED 501#define HTTP_BAD_GATEWAY 502#define HTTP_SERVICE_UNAVAILABLE 503#define HTTP_GATEWAY_TIME_OUT 504#define HTTP_VERSION_NOT_SUPPORTED 505#define HTTP_VARIANT_ALSO_VARIES 506#define HTTP_INSUFFICIENT_STORAGE 507#define HTTP_NOT_EXTENDED 510static const char * const status_lines[RESPONSE_CODES] ={ "100 Continue", "101 Switching Protocols", "102 Processing",#define LEVEL_200 3 "200 OK", "201 Created", "202 Accepted", "203 Non-Authoritative Information", "204 No Content", "205 Reset Content", "206 Partial Content", "207 Multi-Status",#define LEVEL_300 11 "300 Multiple Choices", "301 Moved Permanently", "302 Found", "303 See Other", "304 Not Modified", "305 Use Proxy", "306 unused", "307 Temporary Redirect",#define LEVEL_400 19 "400 Bad Request", "401 Authorization Required", "402 Payment Required", "403 Forbidden", "404 Not Found", "405 Method Not Allowed", "406 Not Acceptable", "407 Proxy Authentication Required", "408 Request Time-out", "409 Conflict", "410 Gone", "411 Length Required", "412 Precondition Failed", "413 Request Entity Too Large", "414 Request-URI Too Large", "415 Unsupported Media Type", "416 Requested Range Not Satisfiable", "417 Expectation Failed", "418 unused", "419 unused", "420 unused", "421 unused", "422 Unprocessable Entity", "423 Locked", "424 Failed Dependency", /* This is a hack, but it is required for ap_index_of_response * to work with 426. */ "425 No code", "426 Upgrade Required",#define LEVEL_500 46 "500 Internal Server Error", "501 Method Not Implemented", "502 Bad Gateway", "503 Service Temporarily Unavailable", "504 Gateway Time-out", "505 HTTP Version Not Supported", "506 Variant Also Negotiates", "507 Insufficient Storage", "508 unused", "509 unused", "510 Not Extended"};// 方法处理基类,基本的获取读取文件class CMethodBaseProc{public: CMethodBaseProc() { }; virtual ~CMethodBaseProc() { }; virtual int Process(string &strReqLine); virtual string &GetFileContext() { return m_strFileContext; }protected: string m_strFileContext; };// Get方法的处理class CGetMethodProc: public CMethodBaseProc{public: CGetMethodProc() { } ~CGetMethodProc() { }};// 请求的方法区分,并使用对应的工具类处理// 请求行的处理class CReqLineProc{public: CReqLineProc() { } virtual ~CReqLineProc() { } CReqLineProc(string strReqLine) { m_strReqLine = strReqLine; } int ProcReqMethod(); CGetMethodProc m_stGet; string m_strBodyContext; CMethodBaseProc *m_pMethod;private: string m_strReqLine;};// HTTP 核心处理class CHttpCore{public: CHttpCore() { } ~CHttpCore() { } int ReqParse(int Id, string &strReqInfo); int Response(int Id, string &strRsp);private: int GetStatusIndex(int RetCode) { if (RetCode >= 500) { return RetCode-500+LEVEL_500; } else if (RetCode >= 400) { return RetCode-400+LEVEL_400; } else if (RetCode >= 300) { return RetCode-300+LEVEL_300; } else if (RetCode >= 200) { return RetCode-200+LEVEL_200; } else { return RetCode-100; } } CReqLineProc *m_pLineProcObj; map m_mapRspInfo;};#endif
 
 类似资料: