forked from ucsd-cse29/lab5-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-server.c
40 lines (28 loc) · 1.06 KB
/
number-server.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
#include "http-server.h"
#include <string.h>
int num = 0;
char const HTTP_404_NOT_FOUND[] = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n";
void handle_404(int client_sock, char *path) {
printf("SERVER LOG: Got request for unrecognized path \"%s\"\n", path);
char response_buff[BUFFER_SIZE];
snprintf(response_buff, BUFFER_SIZE, "Error 404:\r\nUnrecognized path \"%s\"\r\n", path);
// snprintf includes a null-terminator
// TODO: send response back to client?
}
void handle_response(char *request, int client_sock) {
char path[256];
printf("\nSERVER LOG: Got request: \"%s\"\n", request);
// Parse the path out of the request line (limit buffer size; sscanf null-terminates)
if (sscanf(request, "GET %255s", path) != 1) {
printf("Invalid request line\n");
return;
}
handle_404(client_sock, path);
}
int main(int argc, char *argv[]) {
int port = 0;
if(argc >= 2) { // if called with a port number, use that
port = atoi(argv[1]);
}
start_server(&handle_response, port);
}