aboutsummaryrefslogtreecommitdiff
path: root/src/http_server.h
blob: 6175295fc9fc8ef14e83310497baf3d58cb52f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef HTTP_SERVER
#define HTTP_SERVER

#include <string>
#include <map>
#include <ws2tcpip.h>

#include "http_server.h"

#define HTTP_OK "200 OK"

typedef std::map<std::string, std::string> header_map;

class HTTPRequest {
    private:
        SOCKET socket;

        std::string request_type;
        std::string path;
        header_map headers;
        std::string body;


    public:
        HTTPRequest(SOCKET socket);
        ~HTTPRequest();

        std::string get_body() { return this->body; }

        void parse_headers(std::string raw);
        void set_body(std::string body) { this->body = body; }
        size_t content_length();

        void respond(std::string status_code, header_map response_headers, std::string response_body);
        void close();
};

class HTTPServer {
    private:
        SOCKET sock = -1;

        void parse_header();

    public:
        HTTPServer(unsigned long addr, unsigned short port);
        ~HTTPServer();

        SOCKET get_socket() { return this->sock; }

        void close();
        HTTPRequest* receive_request();
};

#endif