Guest User

Untitled

a guest
Jul 7th, 2016
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////   PARSE   //////////////////////////////////////////////////
  2.  
  3. if (line == NULL)
  4.     {
  5.         return false;
  6.     }
  7.     //allocate memory for copy of line then copy line    
  8.     char* linecopy = malloc(sizeof(line));
  9.     char* method = malloc(sizeof(char)*3);
  10.     char* request_target = malloc(sizeof(LimitRequestLine) + 1);
  11.     char* http_version = malloc(sizeof(char) * 8);
  12.     char* tempquery = malloc(sizeof(LimitRequestLine) + 1);
  13.    
  14.     //copy input
  15.     strcpy(linecopy, line);
  16.  
  17.     //store line in method - end at space
  18.     for (int i = 0; linecopy[i] != ' '; i++)
  19.     {
  20.         method[i] = linecopy[i];
  21.     }
  22.    
  23.     //compare method
  24.     if (strcmp(method, "GET") != 0)
  25.     {
  26.         error(405);
  27.         return false;
  28.     }
  29.    
  30.     //determine spaces in line
  31.     char* space1 = strchr(linecopy, ' ') + 1;
  32.     char* space2 = strchr (space1, ' ');
  33.    
  34.     //ensure request-target begins with /
  35.     if (space1[0] != '/')
  36.     {
  37.         error(501);
  38.         return false;
  39.     }
  40.  
  41.     //gather lengths for string equation of request-target length
  42.     int space1len = strlen(space1);
  43.     int space2len = strlen(space2);
  44.     int requestlen = space1len - space2len;
  45.    
  46.     //from the first space store req target
  47.     for (int j = 0; j < requestlen; j++)
  48.     {
  49.         request_target[j] = space1[j];
  50.        
  51.         //check for "
  52.         if (j == '\"')
  53.         {
  54.             error(400);
  55.             return false;
  56.         }
  57.     }
  58.    
  59.     //check for " again - I can see the request target in GDB but this check and
  60.     //the previous check both seem to fail check50.
  61.     if (strchr(request_target, '\"') != NULL)
  62.     {
  63.         error(400);
  64.         return false;
  65.     }
  66.  
  67.  //   char* lineend = strchr(linecopy, '\r');
  68.     int linelen = strlen(linecopy) - 2;
  69.     int subtractlen = strlen(space2 + 1);
  70.     int templen = linelen - subtractlen;
  71.     int httplen = linelen - templen;
  72.      
  73.     //start at one to skip 0
  74.     for (int k = 0; k < httplen -1; k++)
  75.     {
  76.         http_version[k] = space2[k];
  77.     }
  78.    
  79.     //compare http version strings
  80.     if (strcmp(http_version, " HTTP/1.1") != 0)
  81.     {  
  82.         error(505);
  83.         return false;
  84.     }
  85.    
  86.     //copy req target to a temp string
  87.     strcpy (tempquery, request_target);
  88.    
  89.     //find the question mark + space and store the pointer to it
  90.     char* findqmark = strstr(tempquery, "? ");
  91.    
  92.     //if qmark and space exist, replace with nothing
  93.     if (findqmark != NULL)
  94.     {
  95.         strcpy(query, "");
  96.     }
  97.    
  98.     //find the query and store the pointer to it.
  99.     char* findq = strchr(request_target, 'q');
  100.    
  101.     //if q is found, copy that to query
  102.     if (findq != NULL)
  103.     {
  104.         strcpy(query, findq);
  105.     }
  106.    
  107.     //else copy entire req target to abs_path
  108.     else strcpy(abs_path, request_target);
  109.    
  110.     //free the dyn mem allocation
  111.     free (linecopy);
  112. free (method);
  113. free (request_target);
  114. free (http_version);
  115. free (tempquery);
  116.    
  117.     return true;
  118. }
  119.  
  120.  
  121. //////////////////////////////////////////////////   LOOKUP   //////////////////////////////////////////////////
  122.  
  123. const char* lookup(const char* path)
  124. {
  125.     if (path == NULL)
  126.     {
  127.         return NULL;
  128.     }
  129.    
  130.     char* fileextension = malloc(sizeof(path));
  131.     fileextension = strrchr(path, '.');
  132.    
  133.     if (fileextension != NULL)
  134.     {
  135.         if (strcasecmp(fileextension, ".css") == 0)
  136.         {
  137.             return "text/css";
  138.         }
  139.        
  140.         else if (strcasecmp(fileextension, ".html") == 0)
  141.         {
  142.             return "text/html";
  143.         }
  144.        
  145.         else if (strcasecmp(fileextension, ".gif") == 0)
  146.         {
  147.             return "image/gif";
  148.         }
  149.        
  150.         else if (strcasecmp(fileextension, ".ico") == 0)
  151.         {
  152.             return "image/x-icon";
  153.         }
  154.        
  155.         else if (strcasecmp(fileextension, ".jpg") == 0)
  156.         {
  157.             return "image/jpeg";
  158.         }
  159.        
  160.         else if (strcasecmp(fileextension, ".js") == 0)
  161.         {
  162.             return "text/javascript";
  163.         }
  164.        
  165.         else if (strcasecmp(fileextension, ".php") == 0)
  166.         {
  167.             return "text/x-php";
  168.         }
  169.        
  170.         else if (strcasecmp(fileextension, ".png") == 0)
  171.         {
  172.             return "image/png";
  173.         }
  174.        
  175.         else
  176.         {
  177.             return NULL;
  178.         }
  179.     }
  180.    
  181.     free(fileextension);
  182.  
  183.     return NULL;
  184. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×