http_parser.hpp 1.6 KB
Newer Older
Thomas Bailleux's avatar
Thomas Bailleux committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004

 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 0. You just DO WHAT THE FUCK YOU WANT TO.



 Author: zadig
*/

#ifndef SMS_MANAGER_HTTP_PARSER_HPP
#define SMS_MANAGER_HTTP_PARSER_HPP

#include <sstream>
25
#include <string>
Thomas Bailleux's avatar
Thomas Bailleux committed
26
27
28
29
30
31
32
33
34
#include <vector>
#include <boost/algorithm/string.hpp>

#define NO_ERROR 0x0
#define BAD_METHOD -0x1
#define BAD_REQUEST -0x2

namespace sms {

35
/* An HTTP Header */
Thomas Bailleux's avatar
Thomas Bailleux committed
36
37
38
39
40
41
class Header {
  public:
    std::string m_name;
    std::string m_value;
};

42
43
/* Parser for HTTP Request.
 * This parser extracts the method, the path and the header. It does not support the content and the query. */
Thomas Bailleux's avatar
Thomas Bailleux committed
44
class HttpParser {
45
46
47
48
49
50
51
52
53
54
55

  public:
    /* Construct a parser from the buffer to parse. */
    HttpParser(char *buffer, size_t buffer_length): m_buffer(buffer), m_buffer_length(buffer_length) {};

    /* Parse the buffer. */
    int parse();

    /* Get the headers. */
    std::vector<Header>& get_headers();

Thomas Bailleux's avatar
Thomas Bailleux committed
56
57
58
59
60
61
62
63
64
  private:
    std::string m_method;
    std::string m_path;
    std::vector<Header> m_headers;
    char *m_buffer;
    size_t m_buffer_length;

    int parse_first_line(std::string& line);
    int parse_header(std::string& line);
65
    void uri_decode(std::string& data);
Thomas Bailleux's avatar
Thomas Bailleux committed
66
67
68
69
70
71
};

} /* namespace sms */


#endif //SMS_MANAGER_HTTP_PARSER_HPP