Commits

Alex Hixon committed 921080d

Split into multiple files; link against flag.h

Comments (0)

Files changed (3)

 // TimSchmid and Alexander.Hixon, tims and 
 // wed18-drum, ruperts 
 // 19 apr 2011 
-// flag server FIXME
+// flag server
 
-#include "lib.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+
+#include "flag.h"
 
 int main (int argc, char *argv[]) {
-    
     printf ("************************************\n");
     printf ("Starting Flag Server %f\n", SIMPLE_SERVER_VERSION);
     printf ("Promoting national pride since 2011\n");   
     return EXIT_SUCCESS; 
 }  
 
-int stripPath (char *request, char *path, int maxLen) {
-    
-    int startPath, endPath, pathLen;
-    int validPath = TRUE;
-    char *delimArray;
-        
-    // locates the start of the path
-    delimArray = strchr (request,' ');
-    startPath = delimArray - request;
-        
-    // increment startPath by 2 to get rid of ' /' prefix
-    startPath += 2;
-        
-    // locates the end of the path by looking after the first ' '
-    delimArray = strchr (delimArray + 1,' ');
-    endPath = delimArray - request;
-        
-    // decrement endPath by 1 to get rid of ' ' suffix
-    endPath -= 1;
-        
-    pathLen = endPath - startPath;
- 
-    // abort if the URL is too long
-    if (pathLen > maxLen) {
-        validPath = FALSE;
-    } else {
-        
-        int i = 0; //FIXME is this bad style?
-        while (i <= pathLen) {
-            path[i] = request[startPath+i];
-            i++;
-        }
-        
-        // closes off the string
-        path[i] = '\0'; 
-    }
-       
-    return validPath;
-}
-
 void servePage (int socket, char *path, int pathLen) {
     
     if (strcmp (path, "1.bmp") == 0) {
     serveHTTPHeader (socket, "200 OK", FILE_SIZE, "image/bmp");
 }
 
-void serveHTTPHeader (int socket, char *statusMessage, int length,
-                     char *contentType) {
-    
-    
-    char message[MAX_HEADER_SIZE];
-    char *format = "HTTP/1.0 %s\n"
-        "Content-Type: %s\n"
-        "Content-Length: %o\n" //length in octets
-        "\n";
-  
-    snprintf (message, sizeof (message), format, statusMessage, 
-              contentType, length);
-              write (socket, message, strlen (message));
-}
-
 void serveBitmapHeader (bmp imageInfo, int socket) {
     char header[HEADER_SIZE] = {
         
     
     write (socket, header, HEADER_SIZE);
 }
-
-
-
-// start the server listening on the specified port number
-int makeServerSocket (int portNumber) { 
-    
-    // create socket
-    int serverSocket = socket (AF_INET, SOCK_STREAM, 0);
-    assert (serverSocket >= 0);   
-    // error opening socket
-    
-    // bind socket to listening port
-    struct sockaddr_in serverAddress;
-    bzero ((char *) &serverAddress, sizeof (serverAddress));
-    
-    serverAddress.sin_family      = AF_INET;
-    serverAddress.sin_addr.s_addr = INADDR_ANY;
-    serverAddress.sin_port        = htons (portNumber);
-    
-    // let the server start immediately after a previous shutdown
-    int optionValue = 1;
-    setsockopt (serverSocket, SOL_SOCKET, SO_REUSEADDR, &optionValue, 
-                sizeof(int));
-                
-    int bindSuccess = bind (serverSocket, (struct sockaddr *)
-                      &serverAddress, sizeof (serverAddress));
-                      
-    assert (bindSuccess >= 0);
-    // if this assert fails wait a short while to let the operating 
-    // system clear the port before trying again
-                      
-    return serverSocket;
-}
-
-// wait for a browser to request a connection,
-// returns the socket on which the conversation will take place
-int waitForConnection (int serverSocket) {
-    
-    // listen for a connection
-    const int serverMaxBacklog = 10;
-    listen (serverSocket, serverMaxBacklog);
-    
-    // accept the connection
-    struct sockaddr_in clientAddress;
-    socklen_t clientLen = sizeof (clientAddress);
-    int connectionSocket = accept (serverSocket, (struct sockaddr *)
-                           &clientAddress, &clientLen);
-            
-    assert (connectionSocket >= 0);
-    // error on accept
-            
-    return (connectionSocket);
-}
 // 19 apr 2011 
 // flag server FIXME
 
-//implemented libraries
-#include <stdlib.h>
-#include <stdio.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <assert.h>
-#include <unistd.h>
-
 // boolean operators
 #define FALSE 0
 #define TRUE 1
+#include <netinet/in.h>
+#include <assert.h>
+#include <unistd.h>
+
+#include "flag.h"
+
+// start the server listening on the specified port number
+int makeServerSocket (int portNumber) { 
+    
+    // create socket
+    int serverSocket = socket (AF_INET, SOCK_STREAM, 0);
+    assert (serverSocket >= 0);   
+    // error opening socket
+    
+    // bind socket to listening port
+    struct sockaddr_in serverAddress;
+    bzero ((char *) &serverAddress, sizeof (serverAddress));
+    
+    serverAddress.sin_family      = AF_INET;
+    serverAddress.sin_addr.s_addr = INADDR_ANY;
+    serverAddress.sin_port        = htons (portNumber);
+    
+    // let the server start immediately after a previous shutdown
+    int optionValue = 1;
+    setsockopt (serverSocket, SOL_SOCKET, SO_REUSEADDR, &optionValue, 
+                sizeof(int));
+                
+    int bindSuccess = bind (serverSocket, (struct sockaddr *)
+                      &serverAddress, sizeof (serverAddress));
+                      
+    assert (bindSuccess >= 0);
+    // if this assert fails wait a short while to let the operating 
+    // system clear the port before trying again
+                      
+    return serverSocket;
+}
+
+// wait for a browser to request a connection,
+// returns the socket on which the conversation will take place
+int waitForConnection (int serverSocket) {
+    
+    // listen for a connection
+    const int serverMaxBacklog = 10;
+    listen (serverSocket, serverMaxBacklog);
+    
+    // accept the connection
+    struct sockaddr_in clientAddress;
+    socklen_t clientLen = sizeof (clientAddress);
+    int connectionSocket = accept (serverSocket, (struct sockaddr *)
+                           &clientAddress, &clientLen);
+            
+    assert (connectionSocket >= 0);
+    // error on accept
+            
+    return (connectionSocket);
+}
+
+void serveHTTPHeader (int socket, char *statusMessage, int length,
+                     char *contentType) {
+    
+    
+    char message[MAX_HEADER_SIZE];
+    char *format = "HTTP/1.0 %s\n"
+        "Content-Type: %s\n"
+        "Content-Length: %o\n" //length in octets
+        "\n";
+  
+    snprintf (message, sizeof (message), format, statusMessage, 
+              contentType, length);
+    
+    write (socket, message, strlen (message));
+}
+
+int stripPath (char *request, char *path, int maxLen) {
+    
+    int startPath, endPath, pathLen;
+    int validPath = TRUE;
+    char *delimArray;
+        
+    // locates the start of the path
+    delimArray = strchr (request,' ');
+    startPath = delimArray - request;
+        
+    // increment startPath by 2 to get rid of ' /' prefix
+    startPath += 2;
+        
+    // locates the end of the path by looking after the first ' '
+    delimArray = strchr (delimArray + 1,' ');
+    endPath = delimArray - request;
+        
+    // decrement endPath by 1 to get rid of ' ' suffix
+    endPath -= 1;
+        
+    pathLen = endPath - startPath;
+ 
+    // abort if the URL is too long
+    if (pathLen > maxLen) {
+        validPath = FALSE;
+    } else {
+        
+        int i = 0; //FIXME is this bad style?
+        while (i <= pathLen) {
+            path[i] = request[startPath+i];
+            i++;
+        }
+        
+        // closes off the string
+        path[i] = '\0'; 
+    }
+       
+    return validPath;
+}