-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp_response.c
More file actions
146 lines (130 loc) · 4.55 KB
/
Copy pathhttp_response.c
File metadata and controls
146 lines (130 loc) · 4.55 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libdill.h>
#include <aio.h>
#include <fcntl.h>
#include "http_response.h"
char* status_text[] = {
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//100s
"Continue", "Switching Protocols", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//200s
"OK", "Created", "Accepted", "Non-Authoratative Information", "No Content",
"Reset Content", "Partial Content", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//300s
"Multiple Choices", "Moved Permanently", "Found", "See Other", "Not Modified",
"Use Proxy", "", "Temporary Redirect", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//400s
"Bad Request", "Unauthorized", "Payment Required", "Forbidden", "Not Found",
"Method Not Allowed", "Not Acceptable", "Proxy Authentication Required",
"Request Timeout", "Conflict",
"Gone", "Length Required", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//500s
"Internal Server Error", "Not Implemented", "Bad Gateway", "Service Unavailable",
"Gateway Timeout", "", "", "", "", ""
};
#define HEADER_TEMPLATE "HTTP/1.1 %d %s\r\nDate: %.24s\r\nConnection: keep-alive\r\nContent-Length: %d\r\n\r\n"
#define RES_TEMPLATE HEADER_TEMPLATE "%.*s"
void http_response_send(http_response_t* response) {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char* buf;
int len = asprintf(
&buf,
RES_TEMPLATE,
response->status_code,
status_text[response->status_code],
asctime(timeinfo),
response->content_length,
response->content_length,
response->content);
bsend(response->dill_handle, buf, len, -1);
free(buf);
}
void http_response_init(http_response_t* response, int dill_handle) {
memset(response, 0, sizeof(http_response_t));
response->dill_handle = dill_handle;
response->status_code = 200;
}
void http_response_set_status(http_response_t* response, int status_code) {
response->status_code = status_code;
}
void http_response_set_content(http_response_t* response, const char* content, int content_length) {
response->content = content;
response->content_length = content_length;
}
int read_file(const char* path, char** ptr, int* len) {
int fd = open(path, O_RDONLY);
int chunk_size = 2048, err, ret;
*ptr = NULL;
*len = 0;
struct aiocb aiocb;
do {
memset(&aiocb, 0, sizeof(aiocb));
*ptr = realloc(*ptr, chunk_size + *len);
aiocb.aio_fildes = fd;
aiocb.aio_nbytes = chunk_size;
aiocb.aio_buf = *ptr + *len;
aiocb.aio_offset = *len;
aio_read(&aiocb);
while ((err = aio_error(&aiocb)) == EINPROGRESS) yield();
ret = aio_return(&aiocb);
*len += ret;
} while (ret == chunk_size);
close(fd);
return 0;
}
void http_response_send_file(http_response_t* response, const char* path) {
char* ptr;
int len;
read_file(path, &ptr, &len);
http_response_set_content(response, ptr, len);
http_response_send(response);
free(ptr);
}