-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.cpp
63 lines (53 loc) · 1.22 KB
/
response.cpp
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
#include "response.h"
#include <time.h>
Response::Response(int code)
{
response = evbuffer_new();
evbuffer_add_printf(response, "HTTP/1.1 %d %s\r\n", code, getNameByCode(code));
addHeader("Server", "LookServer/0.1");
//form date value
time_t rawtime;
struct tm * ptm;
time (&rawtime);
ptm = gmtime (&rawtime);
char date[80];
strftime (date, 80, "%a, %d %b %Y %H:%M:%S %Z", ptm);
addHeader("Date", date);
addHeader("Connection", "Close");
}
Response::~Response()
{
}
void Response::addHeader(const char *name, const char *value)
{
evbuffer_add_printf(response, "%s: %s\r\n", name, value);
}
void Response::addHeader(const char *name, int value)
{
evbuffer_add_printf(response, "%s: %d\r\n", name, value);
}
evbuffer *Response::getRawResponse()
{
evbuffer_add_printf(response, "\r\n");
return response;
}
const char *Response::getNameByCode(int code)
{
switch(code) {
case 200:
return "OK";
break;
case 403:
return "Forbidden";
break;
case 404:
return "Not Found";
break;
case 405:
return "Method Not Allowed";
break;
case 400:
return "Bad Request";
break;
}
}