-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_service.hpp
53 lines (41 loc) · 1.82 KB
/
http_service.hpp
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
#ifndef HTTP_SERVICE_H
#define HTTP_SERVICE_H
#include <string>
#include <map>
#include <fstream>
#include "stream_server.hpp"
#include "http_request.hpp"
//=============================================================================
// An http service (base class). Default behaviour: serve static files with
// uploading and files listing support
//=============================================================================
class http_service {
public:
http_service(std::string default_files_dir = ".")
: files_dir(default_files_dir)
{}
// HTTP supported commands
virtual void get(peer_connection& conn, const http_request& request);
virtual void head(peer_connection& conn, const http_request& request);
virtual void post(peer_connection& conn, const http_request& request);
virtual void put(peer_connection& conn, const http_request& request);
virtual void del(peer_connection& conn, const http_request& request);
virtual void options(peer_connection& conn, const http_request& request);
// request dispatcher
void do_service(peer_connection& conn, const http_request& request);
std::string get_error_code_str(int error_code);
protected:
// Helper function to build and send an HTTP successfull response
void send_response(peer_connection& conn,
std::string protocol, int error_code,
std::map< std::string, std::string > const & headers,
std::string body="");
// Helper function to build and send an HTTP error response
void respond_error(peer_connection& conn, int error_code);
std::string server_header() const {
return "Server: Simple HTTP server 1.0";
}
// Default working directory (default: server's current directory)
std::string files_dir;
};
#endif