| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <arpa/inet.h> |
| 8 |
#include <assert.h> |
| 9 |
#include <errno.h> |
| 10 |
#include <netinet/in.h> |
| 11 |
#include <signal.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/socket.h> |
| 16 |
#include <sys/wait.h> |
| 17 |
#include <unistd.h> |
| 18 |
|
| 19 |
#include "server.h" |
| 20 |
|
| 21 |
/* HTTP response and header for a successful request. */ |
| 22 |
|
| 23 |
static char* ok_response = |
| 24 |
"HTTP/1.0 200 OK\n" |
| 25 |
"Content-type: text/html\n" |
| 26 |
"\n"; |
| 27 |
|
| 28 |
/* HTTP response, header, and body indicating that the we didn't |
| 29 |
understand the request. */ |
| 30 |
|
| 31 |
static char* bad_request_response = |
| 32 |
"HTTP/1.0 400 Bad Request\n" |
| 33 |
"Content-type: text/html\n" |
| 34 |
"\n" |
| 35 |
"<html>\n" |
| 36 |
" <body>\n" |
| 37 |
" <h1>Bad Request</h1>\n" |
| 38 |
" <p>This server did not understand your request.</p>\n" |
| 39 |
" </body>\n" |
| 40 |
"</html>\n"; |
| 41 |
|
| 42 |
/* HTTP response, header, and body template indicating that the |
| 43 |
requested document was not found. */ |
| 44 |
|
| 45 |
static char* not_found_response_template = |
| 46 |
"HTTP/1.0 404 Not Found\n" |
| 47 |
"Content-type: text/html\n" |
| 48 |
"\n" |
| 49 |
"<html>\n" |
| 50 |
" <body>\n" |
| 51 |
" <h1>Not Found</h1>\n" |
| 52 |
" <p>The requested URL %s was not found on this server.</p>\n" |
| 53 |
" </body>\n" |
| 54 |
"</html>\n"; |
| 55 |
|
| 56 |
/* HTTP response, header, and body template indicating that the |
| 57 |
method was not understood. */ |
| 58 |
|
| 59 |
static char* bad_method_response_template = |
| 60 |
"HTTP/1.0 501 Method Not Implemented\n" |
| 61 |
"Content-type: text/html\n" |
| 62 |
"\n" |
| 63 |
"<html>\n" |
| 64 |
" <body>\n" |
| 65 |
" <h1>Method Not Implemented</h1>\n" |
| 66 |
" <p>The method %s is not implemented by this server.</p>\n" |
| 67 |
" </body>\n" |
| 68 |
"</html>\n"; |
| 69 |
|
| 70 |
/* Handler for SIGCHLD, to clean up child processes that have |
| 71 |
terminated. */ |
| 72 |
|
| 73 |
static void clean_up_child_process (int signal_number) |
| 74 |
{ |
| 75 |
int status; |
| 76 |
wait (&status); |
| 77 |
} |
| 78 |
|
| 79 |
/* Process an HTTP "GET" request for PAGE, and send the results to the |
| 80 |
file descriptor CONNECTION_FD. */ |
| 81 |
|
| 82 |
static void handle_get (int connection_fd, const char* page) |
| 83 |
{ |
| 84 |
struct server_module* module = NULL; |
| 85 |
|
| 86 |
/* Make sure the requested page begins with a slash and does not |
| 87 |
contain any additional slashes -- we don't support any |
| 88 |
subdirectories. */ |
| 89 |
if (*page == '/' && strchr (page + 1, '/') == NULL) { |
| 90 |
char module_file_name[64]; |
| 91 |
|
| 92 |
/* The page name looks OK. Construct the module name by appending |
| 93 |
".so" to the page name. */ |
| 94 |
snprintf (module_file_name, sizeof (module_file_name), |
| 95 |
"%s.so", page + 1); |
| 96 |
/* Try to open the module. */ |
| 97 |
module = module_open (module_file_name); |
| 98 |
} |
| 99 |
|
| 100 |
if (module == NULL) { |
| 101 |
/* Either the requested page was malformed, or we couldn't open a |
| 102 |
module with the indicated name. Either way, return the HTTP |
| 103 |
response 404, Not Found. */ |
| 104 |
char response[1024]; |
| 105 |
|
| 106 |
/* Generate the response message. */ |
| 107 |
snprintf (response, sizeof (response), not_found_response_template, page); |
| 108 |
/* Send it to the client. */ |
| 109 |
write (connection_fd, response, strlen (response)); |
| 110 |
} |
| 111 |
else { |
| 112 |
/* The requested module was loaded successfully. */ |
| 113 |
|
| 114 |
/* Send the HTTP response indicating success, and the HTTP header |
| 115 |
for an HTML page. */ |
| 116 |
write (connection_fd, ok_response, strlen (ok_response)); |
| 117 |
/* Invoke the module, which will generate HTML output and send it |
| 118 |
to the client file descriptor. */ |
| 119 |
(*module->generate_function) (connection_fd); |
| 120 |
/* We're done with the module. */ |
| 121 |
module_close (module); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/* Handle a client connection on the file descriptor CONNECTION_FD. */ |
| 126 |
|
| 127 |
static void handle_connection (int connection_fd) |
| 128 |
{ |
| 129 |
char buffer[256]; |
| 130 |
ssize_t bytes_read; |
| 131 |
|
| 132 |
/* Read some data from the client. */ |
| 133 |
bytes_read = read (connection_fd, buffer, sizeof (buffer) - 1); |
| 134 |
if (bytes_read > 0) { |
| 135 |
char method[sizeof (buffer)]; |
| 136 |
char url[sizeof (buffer)]; |
| 137 |
char protocol[sizeof (buffer)]; |
| 138 |
|
| 139 |
/* Some data was read successfully. NUL-terminate the buffer so |
| 140 |
we can use string operations on it. */ |
| 141 |
buffer[bytes_read] = '\0'; |
| 142 |
/* The first line the client sends is the HTTP request, which is |
| 143 |
composed of a method, the requested page, and the protocol |
| 144 |
version. */ |
| 145 |
sscanf (buffer, "%s %s %s", method, url, protocol); |
| 146 |
/* The client may send various header information following the |
| 147 |
request. For this HTTP implementation, we don't care about it. |
| 148 |
However, we need to read any data the client tries to send. Keep |
| 149 |
on reading data until we get to the end of the header, which is |
| 150 |
delimited by a blank line. HTTP specifies CR/LF as the line |
| 151 |
delimiter. */ |
| 152 |
while (strstr (buffer, "\r\n\r\n") == NULL) |
| 153 |
bytes_read = read (connection_fd, buffer, sizeof (buffer)); |
| 154 |
/* Make sure the last read didn't fail. If it did, there's a |
| 155 |
problem with the connection, so give up. */ |
| 156 |
if (bytes_read == -1) { |
| 157 |
close (connection_fd); |
| 158 |
return; |
| 159 |
} |
| 160 |
/* Check the protocol field. We understand HTTP versions 1.0 and |
| 161 |
1.1. */ |
| 162 |
if (strcmp (protocol, "HTTP/1.0") && strcmp (protocol, "HTTP/1.1")) { |
| 163 |
/* We don't understand this protocol. Report a bad response. */ |
| 164 |
write (connection_fd, bad_request_response, |
| 165 |
sizeof (bad_request_response)); |
| 166 |
} |
| 167 |
else if (strcmp (method, "GET")) { |
| 168 |
/* This server only implements the GET method. The client |
| 169 |
specified some other method, so report the failure. */ |
| 170 |
char response[1024]; |
| 171 |
|
| 172 |
snprintf (response, sizeof (response), |
| 173 |
bad_method_response_template, method); |
| 174 |
write (connection_fd, response, strlen (response)); |
| 175 |
} |
| 176 |
else |
| 177 |
/* A valid request. Process it. */ |
| 178 |
handle_get (connection_fd, url); |
| 179 |
} |
| 180 |
else if (bytes_read == 0) |
| 181 |
/* The client closed the connection before sending any data. |
| 182 |
Nothing to do. */ |
| 183 |
; |
| 184 |
else |
| 185 |
/* The call to read failed. */ |
| 186 |
system_error ("read"); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
void server_run (struct in_addr local_address, uint16_t port) |
| 191 |
{ |
| 192 |
struct sockaddr_in socket_address; |
| 193 |
int rval; |
| 194 |
struct sigaction sigchld_action; |
| 195 |
int server_socket; |
| 196 |
|
| 197 |
/* Install a handler for SIGCHLD that cleans up child processes that |
| 198 |
have terminated. */ |
| 199 |
memset (&sigchld_action, 0, sizeof (sigchld_action)); |
| 200 |
sigchld_action.sa_handler = &clean_up_child_process; |
| 201 |
sigaction (SIGCHLD, &sigchld_action, NULL); |
| 202 |
|
| 203 |
/* Create a TCP socket. */ |
| 204 |
server_socket = socket (PF_INET, SOCK_STREAM, 0); |
| 205 |
if (server_socket == -1) |
| 206 |
system_error ("socket"); |
| 207 |
/* Construct a socket address structure for the local address on |
| 208 |
which we want to listen for connections. */ |
| 209 |
memset (&socket_address, 0, sizeof (socket_address)); |
| 210 |
socket_address.sin_family = AF_INET; |
| 211 |
socket_address.sin_port = port; |
| 212 |
socket_address.sin_addr = local_address; |
| 213 |
/* Bind the socket to that address. */ |
| 214 |
rval = bind (server_socket, &socket_address, sizeof (socket_address)); |
| 215 |
if (rval != 0) |
| 216 |
system_error ("bind"); |
| 217 |
/* Instruct the socket to accept connections. */ |
| 218 |
rval = listen (server_socket, 10); |
| 219 |
if (rval != 0) |
| 220 |
system_error ("listen"); |
| 221 |
|
| 222 |
if (verbose) { |
| 223 |
/* In verbose mode, display the local address and port number |
| 224 |
we're listening on. */ |
| 225 |
socklen_t address_length; |
| 226 |
|
| 227 |
/* Find the socket's local address. */ |
| 228 |
address_length = sizeof (socket_address); |
| 229 |
rval = getsockname (server_socket, &socket_address, &address_length); |
| 230 |
assert (rval == 0); |
| 231 |
/* Print a message. The port number needs to be converted from |
| 232 |
network byte order (big endian) to host byte order. */ |
| 233 |
printf ("server listening on %s:%d\n", |
| 234 |
inet_ntoa (socket_address.sin_addr), |
| 235 |
(int) ntohs (socket_address.sin_port)); |
| 236 |
} |
| 237 |
|
| 238 |
/* Loop forever, handling connections. */ |
| 239 |
while (1) { |
| 240 |
struct sockaddr_in remote_address; |
| 241 |
socklen_t address_length; |
| 242 |
int connection; |
| 243 |
pid_t child_pid; |
| 244 |
|
| 245 |
/* Accept a connection. This call blocks until a connection is |
| 246 |
ready. */ |
| 247 |
address_length = sizeof (remote_address); |
| 248 |
connection = accept (server_socket, &remote_address, &address_length); |
| 249 |
if (connection == -1) { |
| 250 |
/* The call to accept failed. */ |
| 251 |
if (errno == EINTR) |
| 252 |
/* The call was interrupted by a signal. Try again. */ |
| 253 |
continue; |
| 254 |
else |
| 255 |
/* Something else went wrong. */ |
| 256 |
system_error ("accept"); |
| 257 |
} |
| 258 |
|
| 259 |
/* We have a connection. Print a message if we're running in |
| 260 |
verbose mode. */ |
| 261 |
if (verbose) { |
| 262 |
socklen_t address_length; |
| 263 |
|
| 264 |
/* Get the remote address of the connection. */ |
| 265 |
address_length = sizeof (socket_address); |
| 266 |
rval = getpeername (connection, &socket_address, &address_length); |
| 267 |
assert (rval == 0); |
| 268 |
/* Print a message. */ |
| 269 |
printf ("connection accepted from %s\n", |
| 270 |
inet_ntoa (socket_address.sin_addr)); |
| 271 |
} |
| 272 |
|
| 273 |
/* Fork a child process to handle the connection. */ |
| 274 |
child_pid = fork (); |
| 275 |
if (child_pid == 0) { |
| 276 |
/* This is the child process. It shouldn't use stdin or stdout, |
| 277 |
so close them. */ |
| 278 |
close (STDIN_FILENO); |
| 279 |
close (STDOUT_FILENO); |
| 280 |
/* Also this child process shouldn't do anything with the |
| 281 |
listening socket. */ |
| 282 |
close (server_socket); |
| 283 |
/* Handle a request from the connection. We have our own copy |
| 284 |
of the connected socket descriptor. */ |
| 285 |
handle_connection (connection); |
| 286 |
/* All done; close the connection socket, and end the child |
| 287 |
process. */ |
| 288 |
close (connection); |
| 289 |
exit (0); |
| 290 |
} |
| 291 |
else if (child_pid > 0) { |
| 292 |
/* This is the parent process. The child process handles the |
| 293 |
connection, so we don't need our copy of the connected socket |
| 294 |
descriptor. Close it. Then continue with the loop and |
| 295 |
accept another connection. */ |
| 296 |
close (connection); |
| 297 |
} |
| 298 |
else |
| 299 |
/* Call to fork failed. */ |
| 300 |
system_error ("fork"); |
| 301 |
} |
| 302 |
} |