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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#define _BSD_SOURCE
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include "../picohttp.h"
#define SENDBUF_LEN 256
struct bufbsdsockData {
char * recvbuf;
size_t recvbuf_len;
size_t recvbuf_pos;
char sendbuf[SENDBUF_LEN];
size_t sendbuf_pos;
int fd;
};
int bufbsdsock_read(size_t count, char *buf, void *data_)
{
struct bufbsdsockData *data = data_;
ssize_t rb = 0;
ssize_t r = 0;
do {
size_t len = 0;
if( !data->recvbuf ||
data->recvbuf_pos >= data->recvbuf_len ) {
if( data->recvbuf )
free( data->recvbuf );
data->recvbuf_len = 0;
data->recvbuf_pos = 0;
int avail = 0;
do {
struct pollfd pfd = {
.fd = data->fd,
.events = POLLIN | POLLPRI,
.revents = 0
};
int const pret = poll(&pfd, 1, -1);
if( 0 >= pret ) {
return -1;
}
assert(pfd.revents & (POLLIN | POLLPRI));
if( -1 == ioctl(fd, FIONREAD, &avail) ) {
perror("ioctl(FIONREAD)");
return -1;
}
} while( !avail );
data->recvbuf = malloc( avail);
int r;
while( 0 > (r = read(data->fd, data->recvbuf, avail)) )
if( EINTR == errno )
continue;
if( EAGAIN == errno ||
EWOULDBLOCK == errno ) {
usleep(200);
continue;
}
return -1;
}
data->recvbuf_len += r;
}
len = data->recvbuf_len - data->recvbuf_pos;
if( len > count )
len = count;
rb += len;
} while( rb < count );
return rb;
}
int bufbsdsock_write(size_t count, char const *buf, void *data)
{
int fd = *((int*)data);
ssize_t wb = 0;
ssize_t w = 0;
do {
} while( wb < count );
return wb;
}
int16_t bufbsdsock_getch(void *data)
{
char ch;
if( 1 != bufbsdsock_read(1, &ch, data) )
return -1;
return ch;
}
int bufbsdsock_putch(char ch, void *data)
{
return bufbsdsock_write(1, &ch, data);
}
int bufbsdsock_flush(void *data)
{
return 0;
}
int bsdsock_flush(void* data)
{
return 0;
}
int sockfd = -1;
void bye(void)
{
fputs("exiting\n", stderr);
int const one = 1;
/* allows for immediate reuse of address:port
* after program termination */
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
}
void rhRoot(struct picohttpRequest *req)
{
fprintf(stderr, "handling request /%s\n", req->urltail);
req->response.contenttype = "text/html";
char http_test[] = "<html><head><title>handling request /</title></head>\n<body><a href=\"/test\">/test</a></body></html>\n";
picohttpResponseWrite(req, sizeof(http_test)-1, http_test);
}
void rhTest(struct picohttpRequest *req)
{
fprintf(stderr, "handling request /test%s\n", req->urltail);
char http_test[] = "handling request /test";
picohttpResponseWrite(req, sizeof(http_test)-1, http_test);
if(req->urltail) {
picohttpResponseWrite(req, strlen(req->urltail), req->urltail);
}
}
static uint8_t const favicon_ico[] = {
0x00,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x10,0x00,0x01,0x00,0x04,0x00,0x28,0x01,
0x00,0x00,0x16,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x20,0x00,
0x00,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x09,
0x06,0x00,0x21,0x23,0x22,0x00,0x48,0x4a,0x48,0x00,0x70,0x73,0x71,0x00,0x8d,0x90,
0x8e,0x00,0xb4,0xb7,0xb5,0x00,0xd8,0xdc,0xda,0x00,0xfc,0xff,0xfd,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x77,0x77,
0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x76,0x06,0x76,0x06,0x77,0x77,0x77,
0x77,0x74,0x04,0x74,0x04,0x77,0x77,0x77,0x77,0x72,0x42,0x72,0x42,0x77,0x77,0x77,
0x77,0x71,0x70,0x70,0x71,0x77,0x70,0x77,0x77,0x52,0x72,0x32,0x72,0x57,0x70,0x77,
0x77,0x34,0x74,0x04,0x74,0x37,0x70,0x77,0x77,0x16,0x76,0x06,0x76,0x17,0x70,0x40,
0x03,0x77,0x77,0x77,0x77,0x77,0x70,0x26,0x62,0x37,0x77,0x77,0x77,0x77,0x70,0x67,
0x76,0x17,0x77,0x77,0x77,0x77,0x70,0x77,0x77,0x07,0x77,0x77,0x77,0x77,0x70,0x67,
0x76,0x17,0x77,0x77,0x77,0x77,0x70,0x26,0x62,0x37,0x77,0x77,0x77,0x77,0x70,0x40,
0x03,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
void rhFavicon(struct picohttpRequest *req)
{
fprintf(stderr, "handling request /favicon.ico\n");
req->response.contenttype = "image/x-icon";
req->response.contentlength = sizeof(favicon_ico);
picohttpResponseWrite(req, sizeof(favicon_ico), favicon_ico);
}
int main(int argc, char *argv[])
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if( -1 == sockfd ) {
perror("socket");
return -1;
}
#if 0
if( atexit(bye) ) {
return -1;
}
#endif
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(8000),
.sin_addr = 0
};
int const one = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if( -1 == bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) ) {
perror("bind");
return -1;
}
if( -1 == listen(sockfd, 2) ) {
perror("listen");
return -1;
}
for(;;) {
socklen_t addrlen = 0;
int confd = accept(sockfd, (struct sockaddr*)&addr, &addrlen);
if( -1 == confd ) {
if( EAGAIN == errno ||
EWOULDBLOCK == errno ) {
usleep(1000);
continue;
} else {
perror("accept");
return -1;
}
}
struct picohttpIoOps ioops = {
.read = bsdsock_read,
.write = bsdsock_write,
.getch = bsdsock_getch,
.putch = bsdsock_putch,
.flush = bsdsock_flush,
.data = &confd
};
struct picohttpURLRoute routes[] = {
{"/favicon.ico|", 0, rhFavicon, 0, PICOHTTP_METHOD_GET},
{ "/test", 0, rhTest, 16, PICOHTTP_METHOD_GET },
{ "/|", 0, rhRoot, 0, PICOHTTP_METHOD_GET },
{ NULL, 0, 0, 0, 0 }
};
picohttpProcessRequest(&ioops, routes);
shutdown(confd, SHUT_RDWR);
close(confd);
}
return 0;
}
|