Http1Session.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef HTTP1_SESSION_H_
  2. #define HTTP1_SESSION_H_
  3. #include "HttpSession.h"
  4. #include "http_parser.h"
  5. enum http_parser_state {
  6. HP_START_REQ_OR_RES,
  7. HP_MESSAGE_BEGIN, HP_URL,
  8. HP_STATUS,
  9. HP_HEADER_FIELD,
  10. HP_HEADER_VALUE,
  11. HP_HEADERS_COMPLETE,
  12. HP_BODY,
  13. HP_MESSAGE_COMPLETE
  14. };
  15. class Http1Session : public HttpSession {
  16. public:
  17. static http_parser_settings* cbs;
  18. http_parser parser;
  19. http_parser_state state;
  20. HttpPayload* submited;
  21. HttpPayload* parsed;
  22. // tmp
  23. std::string url; // for on_url
  24. std::string header_field; // for on_header_field
  25. std::string header_value; // for on_header_value
  26. std::string sendbuf; // for GetSendData
  27. Http1Session(http_session_type type = HTTP_CLIENT);
  28. virtual ~Http1Session();
  29. void handle_header() {
  30. if (header_field.size() != 0 && header_value.size() != 0) {
  31. parsed->headers[header_field] = header_value;
  32. header_field.clear();
  33. header_value.clear();
  34. }
  35. }
  36. virtual int GetSendData(char** data, size_t* len);
  37. virtual int FeedRecvData(const char* data, size_t len);
  38. virtual bool WantRecv();
  39. // client
  40. // SubmitRequest -> while(GetSendData) {send} -> InitResponse -> do {recv -> FeedRecvData} while(WantRecv)
  41. virtual int SubmitRequest(HttpRequest* req);
  42. virtual int InitResponse(HttpResponse* res);
  43. // server
  44. // InitRequest -> do {recv -> FeedRecvData} while(WantRecv) -> SubmitResponse -> while(GetSendData) {send}
  45. virtual int InitRequest(HttpRequest* req);
  46. virtual int SubmitResponse(HttpResponse* res);
  47. virtual int GetError();
  48. virtual const char* StrError(int error);
  49. };
  50. #endif // HTTP1_SESSION_H_