http.c (676B)
1 #include <stdarg.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 char * 7 requested_file(char *request) { 8 char *file_name; 9 10 /* GET /folder/file.name HTTP/1.1*/ 11 strtok(request, " "); 12 file_name = strtok(NULL, " "); 13 14 return file_name; 15 } 16 17 char * 18 respond(char *payload) { 19 size_t length; 20 char *response, format[] = "HTTP/1.1 200 OK\n" 21 "Content-Type: text/html\n" 22 "Content-Length: %d\n" 23 "Server: bpaul's funny http server\n" 24 "\n" 25 "%s"; 26 27 length = snprintf(NULL, 0, format, strlen(payload), payload); 28 response = calloc(length + 1, 1); 29 30 sprintf(response, format, strlen(payload), payload); 31 32 return response; 33 }