SHARE
TWEET

Untitled

a guest Feb 7th, 2012 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Application: basic-webserver, Web.cpp
  3.  * Author: Project, UNX511, Team A
  4.  * Description: Web class - responsible for creating sockets and listening for connections.
  5.  * A webserver that has been coded from scratch.
  6.  * It uses Linux's socket library to deal with connections.
  7.  *
  8.  * This application is licensed under Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
  9.  * See this for more information: http://creativecommons.org/licenses/by-nc-sa/3.0/
  10.  */
  11.  
  12. #include "Web.h"
  13.  
  14. Web::Web(int port) {
  15.    
  16.    _port = port;
  17.    _socket = socket(AF_INET, SOCK_STREAM, 0);
  18.    if (_socket < 0) {
  19.       log(ERROR, "System Call", "Socket", 0);
  20.    }
  21.    bzero((char *) &_serv_addr, sizeof(_serv_addr));
  22.    _serv_addr.sin_family = AF_INET;
  23.    _serv_addr.sin_addr.s_addr = INADDR_ANY;
  24.    _serv_addr.sin_port = htons(_port);
  25.    if (bind(_socket, (struct sockaddr*) &_serv_addr, sizeof(_serv_addr)) < 0) {
  26.       log(ERROR, "System Call", "Bind", 0);
  27.    }
  28.    if (listen(_socket, 5) == -1){
  29.       log(ERROR, "System Call", "Listen", 0);
  30.    }
  31.    _clilen = sizeof(_cli_addr);
  32. }
  33.  
  34. int Web::run() {
  35.    pid_t pid;
  36.    while (1) {
  37.       _nSocket = accept(_socket, (struct sockaddr *) &_cli_addr, &_clilen);
  38.       if (_nSocket < 0) {
  39.          log(ERROR, "System Call", "Accept", 0);
  40.       }
  41.       pid = fork();
  42.       if (pid < 0) {
  43.          log(ERROR, "System Call", "Fork", 0);
  44.       }
  45.       if (pid == 0) {
  46.          if (close(_socket) == -1) {
  47.             log(ERROR, "System Call", "Close", 0);
  48.          }
  49.          process(_nSocket);
  50.          return EXIT_SUCCESS;
  51.       } else {
  52.          if (close(_nSocket) == -1) {
  53.             log(ERROR, "System Call", "Close", 0);
  54.          }
  55.       }
  56.    }
  57.    if (close(_socket) == -1) {
  58.       log(ERROR, "System Call", "Close", 0);
  59.    }
  60.    return EXIT_SUCCESS;
  61. }
  62.  
  63. void Web::process(int socket) {
  64.    int n;
  65.    int size = 0;
  66.    char buf[BUFSIZE+1];
  67.    bzero(buf, BUFSIZE+1);
  68.  
  69.    n = read(socket, buf, BUFSIZE);
  70.    if (n < 0) {
  71.       log(ERROR, "System Call", "Socket Read", 0);
  72.    }
  73.  
  74.    string response = buf;
  75.    string get = "GET";
  76.    string get_value;
  77.    string cookie = "Cookie";
  78.    string cookie_value;
  79.    string tmp1;
  80.    
  81.    char* response_c = (char*)response.c_str();
  82.    char* pch;
  83.    char* pch2;
  84.    
  85.    pch = strtok(response_c, "\n");
  86.    while(pch){
  87.       tmp1 = pch;
  88.       if(isHttpResponseOf(tmp1, get)){
  89.          pch2 = strtok(pch,  " ");
  90.          pch2 = strtok(NULL, " "); // move once - the get_value is in the second token
  91.          get_value = pch2;
  92.       }
  93.       else if(isHttpResponseOf(tmp1, cookie)){
  94.          pch2 = strtok(pch,  ":");
  95.          cookie_value = pch2;
  96.          cout << cookie_value << endl;
  97.       }
  98.       pch = strtok(NULL, "\n");
  99.    }
  100.  
  101.    string
  102.    html = HTTP_GENERIC_HEADER; // defined in Web.h
  103.    html += "<html><head><title>Basic Web Server</title><script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></script><script type=\"text/javascript\">$(document).ready(function(){$('#msgInput').focus();});</script></head>";
  104.    html += "<html><head><title>Basic Web Server</title></head><body><h1>Basic Web Server</h1>";
  105.    html += "<textarea rows=\"50px\" cols=\"100px\" name=\"msgBox\" id=\"msgBox\" disabled=\"disabled\"></textarea><br /><form><input type=\"text\" size=\"131\" name=\"msgInput\" id=\"msgInput\" />";
  106.    html += "<p>Hi World. Here's the response I got from you: </p><pre>" + response + "</pre>";
  107.    html += "</body></html>\n";
  108.  
  109.    n = write(socket, html.c_str(), html.size());
  110.    if (n < 0) {
  111.       log(ERROR, "System Call", "Socket Write", 0);
  112.    }
  113.    if (close(socket) == -1) {
  114.       log(ERROR, "System Call", "Close", 0);
  115.    }
  116. }
  117.  
  118. bool Web::isHttpResponseOf(string haystack, string needle){
  119.    size_t found = haystack.find(needle);
  120.    return found != string::npos;
  121. }
  122.  
  123. void Web::log(int type, char* s1, char* s2, int num) {
  124.    int fd;
  125.    char logbuffer[BUFSIZE*2];
  126.  
  127.    switch (type) {
  128.    case ERROR: sprintf(logbuffer,"ERROR: %s: %s Errno=%d exiting pid=%d", s1, s2, errno, getpid()); break;
  129.    case LOG: sprintf(logbuffer," INFO: %s: %s: %d", s1, s2, num); break;
  130.    }
  131.    /* no checks here, nothing can be done a failure anyway */
  132.    if((fd = open("server.log", O_CREAT | O_WRONLY | O_APPEND,0644)) >= 0) {
  133.       write(fd, logbuffer, strlen(logbuffer));
  134.       write(fd,"\n",1);      
  135.       close(fd);
  136.    }
  137.    if(type == ERROR) {
  138.       exit(EXIT_FAILURE);
  139.    }
  140. }
  141.  
  142. Web::Web() { }
  143. Web::~Web() {
  144.    close(_socket);
  145.    close(_nSocket);
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top