@CesantaHQ
// Example of a simple Web server. Listening loop
for (;;) {
struct sockaddr_in remote_sa = {};
socklen_t slen = sizeof(remote_sa);
int new_sock = accept(sock, (struct sockaddr *) &remote_sa, &slen);
printf("New connection from %s\n", inet_ntoa(remote_sa.sin_addr));
if (new_sock != -1 && fork() == 0) {
serve_new_connection(fdopen(new_sock, "rb+"));
exit(0);
}
close(new_sock);
}
static void serve_new_connection(FILE *fp) {
char method[10], uri[100], protocol[20];
fscanf(fp, "%s %s %s", method, uri, protocol);
fprintf(fp, "HTTP/1.0 200 OK\r\n\r\nURI: [%s]\n", uri);
}
$ curl '127.0.0.1:8000/hi_there!'
URI: [/hi_there!]
$ curl 127.0.0.1:8000/$(base64 < /dev/urandom | dd bs=1 count=50 2>/dev/null)
URI: [/PpplU0i6FlxHD763gKNqA1Wam4nyWfB38b6B+oMkq+6Qc2kfQw]
$ curl 127.0.0.1:8000/$(base64 < /dev/urandom | dd bs=1 count=150 2>/dev/null)
curl: (52) Empty reply from server
+-----------------+ <-- auld SP
| param1 |
+-----------------+
| param2 |
+-----------------+
| return address |
+-----------------+ <-- FP
| saved registers |
+-----------------+
| local variable1 |
+-----------------+
| local variable2 |
+-----------------+
| local variable3 |
+-----------------+ <-- SP
+-----------------+ <-- auld SP
| param1 |
+-----------------+
| param2 |
+-----------------+
| return address |
+-----------------+ <-- FP
| saved registers |
+-----------------+
| local variable1 |
+-----------------+
| local variable2 |
+-----------------+
| local variable3 |
+-----------------+ <-- SP
contact me at
sergey.lyubka@cesanta.com