MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
http_parser.h
Go to the documentation of this file.
1 
4 #pragma once
5 #include <map>
6 #include <string>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "socket.h"
10 
12 namespace HTTP {
14  class Parser {
15  public:
16  Parser();
17  bool Read(Socket::Connection & conn);
18  bool Read(std::string & strbuf);
19  std::string GetHeader(std::string i);
20  std::string GetVar(std::string i);
21  std::string getUrl();
22  void SetHeader(std::string i, std::string v);
23  void SetHeader(std::string i, long long v);
24  void setCORSHeaders();
25  void SetVar(std::string i, std::string v);
26  void SetBody(std::string s);
27  void SetBody(const char * buffer, int len);
28  std::string & BuildRequest();
29  std::string & BuildResponse();
30  std::string & BuildResponse(std::string code, std::string message);
31  void SendRequest(Socket::Connection & conn);
32  void SendResponse(std::string code, std::string message, Socket::Connection & conn);
33  void StartResponse(std::string code, std::string message, Parser & request, Socket::Connection & conn, bool bufferAllChunks = false);
34  void StartResponse(Parser & request, Socket::Connection & conn, bool bufferAllChunks = false);
35  void Chunkify(const std::string & bodypart, Socket::Connection & conn);
36  void Chunkify(const char * data, unsigned int size, Socket::Connection & conn);
37  void Proxy(Socket::Connection & from, Socket::Connection & to);
38  void Clean();
39  void CleanPreserveHeaders();
40  static std::string urlunescape(const std::string & in);
41  static std::string urlencode(const std::string & in);
42  std::string body;
43  std::string method;
44  std::string url;
45  std::string protocol;
46  unsigned int length;
47  bool headerOnly;
49  //this bool was private
51 
52  private:
54  bool seenReq;
55  bool getChunks;
56  unsigned int doingChunk;
57  bool parse(std::string & HTTPbuffer);
58  void parseVars(std::string data);
59  std::string builder;
60  std::string read_buffer;
61  std::map<std::string, std::string> headers;
62  std::map<std::string, std::string> vars;
63  void Trim(std::string & s);
64  static int unhex(char c);
65  static std::string hex(char dec);
66  };
67 //HTTP::Parser class
68 
69 }//HTTP namespace
void setCORSHeaders()
Sets the neccesary headers to allow Cross Origin Resource Sharing with all domains.
Definition: http_parser.cpp:36
std::string & BuildResponse()
Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
Definition: http_parser.cpp:119
bool sendingChunks
Definition: http_parser.h:50
void SendResponse(std::string code, std::string message, Socket::Connection &conn)
Creates and sends a valid HTTP 1.0 or 1.1 response.
Definition: http_parser.cpp:130
std::string GetHeader(std::string i)
Returns header i, if set.
Definition: http_parser.cpp:307
bool bufferChunks
Definition: http_parser.h:48
bool Read(Socket::Connection &conn)
Attempt to read a whole HTTP request or response from a Socket::Connection.
Definition: http_parser.cpp:345
void SetBody(std::string s)
Function that sets the body of a response or request, along with the correct Content-Length header...
Definition: http_parser.cpp:283
void StartResponse(std::string code, std::string message, Parser &request, Socket::Connection &conn, bool bufferAllChunks=false)
Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request.
Definition: http_parser.cpp:157
std::string builder
Definition: http_parser.h:59
std::string GetVar(std::string i)
Returns POST variable i, if set.
Definition: http_parser.cpp:311
void Clean()
Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage...
Definition: http_parser.cpp:15
std::string & BuildRequest()
Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending.
Definition: http_parser.cpp:52
std::string url
Definition: http_parser.h:44
std::map< std::string, std::string > vars
Definition: http_parser.h:62
static int unhex(char c)
Helper function for urlunescape.
Definition: http_parser.cpp:639
Holds all HTTP processing related code.
Definition: http_parser.h:12
Simple class for reading and writing HTTP 1.0 and 1.1.
Definition: http_parser.h:14
void SetVar(std::string i, std::string v)
Sets POST variable i to string value v.
Definition: http_parser.cpp:331
void parseVars(std::string data)
Parses GET or POST-style variable data.
Definition: http_parser.cpp:530
void Trim(std::string &s)
Trims any whitespace at the front or back of the string.
Definition: http_parser.cpp:271
std::string getUrl()
Returns header i, if set.
Definition: http_parser.cpp:298
bool seenHeaders
Definition: http_parser.h:53
bool parse(std::string &HTTPbuffer)
Attempt to read a whole HTTP response or request from a data buffer.
Definition: http_parser.cpp:385
static std::string hex(char dec)
Helper function for urlescape.
Definition: http_parser.cpp:661
unsigned int doingChunk
Definition: http_parser.h:56
static std::string urlencode(const std::string &in)
URLencodes std::string data.
Definition: http_parser.cpp:644
std::string protocol
Definition: http_parser.h:45
bool headerOnly
If true, do not parse body if the length is a known size.
Definition: http_parser.h:47
void CleanPreserveHeaders()
Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage...
Definition: http_parser.cpp:21
unsigned int length
Definition: http_parser.h:46
bool seenReq
Definition: http_parser.h:54
bool getChunks
Definition: http_parser.h:55
static std::string urlunescape(const std::string &in)
Unescapes URLencoded std::string data.
Definition: http_parser.cpp:612
std::map< std::string, std::string > headers
Definition: http_parser.h:61
std::string read_buffer
Definition: http_parser.h:60
void Proxy(Socket::Connection &from, Socket::Connection &to)
After receiving a header with this object, and after a call with SendResponse/SendRequest with this o...
Definition: http_parser.cpp:187
This class is for easy communicating through sockets, either TCP or Unix.
Definition: socket.h:47
Parser()
This constructor creates an empty HTTP::Parser, ready for use for either reading or writing...
Definition: http_parser.cpp:9
std::string method
Definition: http_parser.h:43
std::string body
Definition: http_parser.h:42
void SendRequest(Socket::Connection &conn)
Creates and sends a valid HTTP 1.0 or 1.1 request.
Definition: http_parser.cpp:71
void Chunkify(const std::string &bodypart, Socket::Connection &conn)
Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise.
Definition: http_parser.cpp:563
void SetHeader(std::string i, std::string v)
Sets header i to string value v.
Definition: http_parser.cpp:316