-
Notifications
You must be signed in to change notification settings - Fork 23
/
httpd.c
170 lines (144 loc) · 4.85 KB
/
httpd.c
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
// simplified from: http://blog.manula.org/2011/05/writing-simple-web-server-in-c.html
#ifdef UNIX
#include <netinet/in.h> // missing on esp-open-rtos
#include <fcntl.h> // duplicate def on esp-open-rtos
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "compat.h"
#define BUFSIZE 512
#define MAXQUEUE 10
// consider looking at using something that handles post
// - https://github.com/sheinz/simple-httpd/blob/master/httpd.c
//
// > 0, good socket: httpd_next(s)... close(s)
//
// < 0 error:
// == -1, socket()
// == -2, setsockopt()
// == -3, bind()
// == -4, listen()
int httpd_init(int port) {
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) printf("SOCKET=%d\n", s);
if (s < 0) return -1;
// non-block on listen and accept
fcntl(s, F_SETFL, O_NONBLOCK);
int enable = 1;
int o = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
if (o < 0) printf("SETSOCKOPT=%d\n", o);
if (o < 0) return -2;
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(s, (struct sockaddr*) &address, sizeof(address)) != 0) return -3;
int l = listen(s, MAXQUEUE);
if (l < 0) return -4;
return s;
}
// simple implementation of getline for fd instead of FILE*
// NOTE: this one does NOT include the \n at end
int fdgetline(char** b, int* len, int fd) {
int n = 0;
while (1) {
if (n + 1 > *len) {
*len *= 1.3;
*b = realloc(*b, *len);
}
if (read(fd, *b + n, 1) < 1) { // eof
if (!n)
return -1; // no trailing line
else
break; // have trailing line (no \n)
}
char c = *(*b + n);
if (c == '\n') break; // new line
if (c != '\r') n++; // store if not cr
}
*(*b + n) = 0;
return n;
}
int httpd_next(int s, httpd_header emit_header, httpd_body emit_body, httpd_response emit_response) {
struct sockaddr_in address;
socklen_t addrlen = sizeof(address);
int req = accept(s, (struct sockaddr*) &address, &addrlen);
//if (req < 0) printf("ACCEPT=%d, errno=%d\n", req, errno);
if (req < 0) return 0;
int len = BUFSIZE;
char *buffer = malloc(len);
// -- process request line
if (fdgetline(&buffer, &len, req) <= 0) return 0;
char method[8];
strncpy(method, strtok(buffer, " "), 8);
method[7] = 0;
char *path = strdup(strtok(NULL, " "));
// -- process headers
int expectedsize = -1;
while (fdgetline(&buffer, &len, req) > 0) {
if (emit_header) emit_header(buffer, method, path);
if (strcmp(buffer, "Content-Length: ") == 0) {
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
// http://stackoverflow.com/questions/2773396/whats-the-content-length-field-in-http-header
strtok(buffer, " ");
expectedsize = atoi(strtok(NULL, " "));
}
}
if (emit_header) emit_header(NULL, method, path);
// TODO: handle POST get parameters, also add paramter getter for path
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5
// may need to handle different encodings...?
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7
// -- process body
int bodycount = 0;
// stop reading when got expectedsize bytes,
// otherwise it'll block as browser keepalive doesn't close, thus no EOF
int n = 1;
len--; // remove capacity for 0 termination
while (n > 0 && bodycount < expectedsize) {
int remaining = expectedsize - bodycount;
int n = (len > remaining) ? remaining : len;
n = read(req, buffer, n);
if (n < 0) break; // error
buffer[n] = 0;
bodycount += n;
if (n > 0 && emit_body) emit_body(buffer, method, path);
}
free(buffer);
if (emit_body) emit_body(NULL, method, path);
if (emit_response) emit_response(req, method, path);
if (path) free(path);
close(req);
return 1;
}
// simple for test
static void header(char* buff, char* method, char* path) {
printf("HEADER: %s\n", buff);
}
static void body(char* buff, char* method, char* path) {
printf("BODY: %s\n", buff);
}
static void response(int req, char* method, char* path) {
static char* hello = "HELLO DUDE!\n";
// TODO: loop until all of the string written?
int ignore = write(req, hello, strlen(hello));
ignore++; // use it!
printf("------------------------------ %s %s\n\n", method, path);
}
void httpd_loop(int s) {
while (1) {
httpd_next(s, header, body, response);
}
}
#ifdef HTTPD_MAIN
int main() {
int s = httpd_init(1111);
if (s < 0 ) { printf("ERROR.errno=%d\n", errno); return -1; }
}
#endif