-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathhttp.hpp
More file actions
91 lines (82 loc) · 2.09 KB
/
Copy pathhttp.hpp
File metadata and controls
91 lines (82 loc) · 2.09 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* (c) William Edwards, 2011
Using the Simplified BSD License. See LICENSE file for details */
#ifndef HTTP_HPP
#define HTTP_HPP
#include "task.hpp"
class HttpError;
void upper(char* s); // in-place
void lower(char* s); // in-place
class HttpServerConnection: private Task {
public:
void dump_context(FILE* out) const;
using Task::construct;
using Task::close;
protected:
friend class HttpError;
HttpServerConnection(Scheduler& scheduler,FD accept_fd);
void do_construct();
void gracefulClose(const char* reason=NULL);
// callbacks when a request comes in
virtual void on_request(const char* method,const char* uri) {}
virtual void on_header(const char* header,const char* value) {}
virtual void on_body() {}
virtual void on_data(const void* chunk,size_t len) {}
// to respond
void writeResponseCode(int code,const char* message);
void writeHeader(const char* header,const char* value);
void write(const void* ptr,size_t len);
void write(const char* str);
void writef(const char* fmt,...);
void finish();
protected:
enum {
HTTP_0_9,
HTTP_1_0,
HTTP_1_1,
} version;
char uri[1024];
InLine<1024*5> line;
bool keep_alive;
private:
void read();
void disconnected();
inline void finishHeader();
private:
enum {
LINE,
HEADER,
BODY,
FINISHED,
} read_state, write_state;
bool in_encoding_chunked, out_encoding_chunked;
int in_content_length; //-1 means not known
int count;
};
class HttpError: private HalfClose {
public:
static const char* const ENotFound;
static const char* const ERequestURITooLong;
static const char* const ERequestEntityTooLarge;
static const char* const EMethodNotAllowed;
static const char* const EPreconditionFailed;
static const char* const EBadRequest;
static void Throw(const char* msg,HttpServerConnection& client);
};
class HttpParams {
public:
HttpParams(char* params);
const char* key() const;
const char* value() const;
void reset();
bool next();
static void UnitTest(const char* params);
private:
bool decode(char* p);
void reencode(char* p,int len);
void restore();
private:
char* params;
int len;
int k, v, n;
};
#endif //HTTP_HPP