3 * @purpose Test implementing a minimalistic webserver
7 #define _POSIX_C_SOURCE 200809L // needed for some low level POSIX stuff to work
9 // --- Standard headers --- //
12 #include <string.h> // string helper functions
13 #include <wctype.h> // wide character classication (white space)
14 #include <signal.h> // for signal handling
16 // --- Custom headers --- //
17 #include "log.h" // C functions to handle logging
18 #include "options.h" // Options structure
19 #include "network.h" // C functions to handle low level networking
21 // --- Variable definitions --- //
22 Options options; // declared in "options.h"
24 // --- Function definitions --- //
27 * @funct ParseArguments
28 * @purpose Parse Arguments and setup the global options variable
29 * @param argc - Num args
30 * @param argv - Array of args
32 void ParseArguments(int argc, char ** argv)
34 options.program = argv[0];
35 options.verbosity = LOGDEBUG;
36 options.port = 8080; // Using 8080 instead of 80 for now because to use 80 you have to run the program as root
37 log_print(LOGDEBUG, "ParseArguments", "Called as %s with %d arguments.", options.program, argc);
41 * @funct SignalHandler
42 * @purpose Handle signals
43 * @param sig - The signal
45 void SignalHandler(int sig)
47 // At the moment just always exit.
48 // Call `exit` so that Cleanup will be called to... clean up.
49 log_print(LOGWARN, "SignalHandler", "Got signal %d (%s). Exiting.", sig, strsignal(sig));
55 * @purpose Called when program exits
59 log_print(LOGDEBUG, "Cleanup", "Begin cleanup.");
60 Network_close(options.sfd); // close socket
61 Network_close(options.bound_sfd); // unbind
62 log_print(LOGDEBUG, "Cleanup", "Unbound from port %d successfully", options.port);
63 log_print(LOGDEBUG, "Cleanup", "Done.");
70 * @purpose Respond to a GET request
71 * @param request - The request string
72 * @param sfd - Socket to respond through
74 void Get(const char * request, int sfd)
76 log_print(LOGDEBUG, "Get", "Got GET request: %s", request);
77 char response[BUFSIZ];
79 // TODO: Magical low level interfacing stuff!
81 int len = sprintf(response, "Content-type: text/plain\n\nYou requested %s using GET\n", request);
82 write(sfd, response, len);
87 * @purpose Respond to a POST request
88 * @param request - The request string
89 * @param sfd - Socket to respond through
91 void Post(const char * request, int sfd)
93 log_print(LOGDEBUG, "Post", "Got POST request: %s", request);
94 char response[BUFSIZ];
96 // TODO: Magical low level interfacing stuff!
98 int len = sprintf(response, "Content-type: text/plain\n\nYou requested %s using POST\n", request);
99 write(sfd, response, len);
105 * @purpose Main program
106 * @param argc - Num arguments
107 * @param argv - Argument string array
108 * @returns error code (0 for no error)
111 int main(int argc, char ** argv)
114 ParseArguments(argc, argv);
115 // Set Cleanup to be called on program exit
118 // Setup signal handlers
119 int signals_to_handle[] = {SIGTERM, SIGINT, SIGHUP, SIGPIPE};
120 for (int i = 0; i < sizeof(signals_to_handle)/sizeof(int); ++i)
122 int s = signals_to_handle[i];
123 if (signal(s, SignalHandler) == SIG_ERR)
124 error("main", "Setting signal handler for %d (%s): %s", s, strsignal(s), strerror(errno));
128 options.bound_sfd = Network_server_bind(options.port, &(options.port));
129 log_print(LOGDEBUG, "main", "Bound to port %d succesfully", options.port);
133 // Listen for a client
134 options.sfd = Network_server_listen(options.bound_sfd, NULL);
136 log_print(LOGDEBUG, "main", "Connected to client");
138 char buffer[BUFSIZ]; //NOTE: Won't be able to respond to requests longer than BUFSIZ
140 int len = read(options.sfd, buffer, sizeof(buffer));
141 log_print(LOGDEBUG, "main", "Read %d characters. Buffer is %s", len, buffer);
144 for (int i = 0; i < sizeof(buffer) && buffer[i] != '\0'; ++i)
146 // Look for "GET" or "POST" followed by a whitespace
147 if (iswspace(buffer[i])) // whitespace
149 while (iswspace(buffer[++i]) && buffer[i] != '\0'); // Skip whitespace
150 char * req = buffer+i; // set request string
151 buffer[i] = '\0'; // terminate request type
152 if (strcmp("GET", buffer) == 0) // Compare with "GET"
154 Get(req, options.sfd);
156 else if (strcmp("POST", buffer) == 0) // Compare with "POST"
158 Post(req, options.sfd);
160 else // Unknown request
162 log_print(LOGWARN, "main", "Unrecognised request type %s (request %s)", buffer, req);
163 char response[] = "Content-type: text/plain\n\nError: Unrecognised request";
164 write(options.sfd, response, sizeof(response));
171 Network_close(options.sfd);
172 log_print(LOGDEBUG, "main", "Closed connection to client");