Guest User

Untitled

a guest
Jul 20th, 2016
95
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool parse(const char* line, char* abs_path, char* query)
  2. {
  3.      // line = "method space1 request-target space2 HTTP-version CRLF"
  4.     if (line == NULL)
  5.     {
  6.         return false;
  7.     }
  8.    
  9.     //allocate memory for copy of line then copy line    
  10.     char* linecopy = malloc(sizeof(strlen(line)));
  11.     char* method = malloc(sizeof(char)*4);
  12.     char* request_target = malloc(sizeof(LimitRequestLine) + 1);
  13.     char* http_version = malloc(sizeof(char) * 12);
  14.     char* tempquery = malloc(sizeof(strlen(query) + 1));
  15.    
  16.     //copy input
  17.     strcpy(linecopy, line);
  18.    
  19.     //determine spaces in line
  20.     char* space1 = strchr (linecopy, ' ');
  21.    
  22.    
  23.     //ensure request-target begins with /
  24.     if (space1[1] != '/')
  25.     {
  26.         error(501);
  27.         return false;
  28.     }
  29.    
  30.     char* requeststart = strchr(space1, '/');
  31.     char* space2 = strchr (requeststart, ' ');
  32.     char* httpstart = strchr (requeststart, 'H');
  33.    
  34.     int linecopylen = strlen(linecopy);
  35.     int methodlen = linecopylen - strlen(space1);
  36.    
  37.     strncpy(method, linecopy, methodlen);
  38.  
  39.     //compare method
  40.     if (strncmp(method, "GET\0", 4) != 0)
  41.     {
  42.         error(405);
  43.         return false;
  44.     }
  45.    
  46.     //gather lengths for string equation of request-target length
  47.     int space1len = (strlen(space1));
  48.     int space2len = (strlen(space2) + 1);
  49.     int requestlen = space1len - space2len;
  50.    
  51.     strncpy(request_target, requeststart, requestlen);
  52.    
  53.     //check for "
  54.     if (strchr(request_target, '\"'))
  55.     {
  56.         error(400);
  57.         return false;
  58.     }
  59.  
  60.     int subtractlen = strlen(space2) + 1;
  61.     int templen = linecopylen - subtractlen;
  62.     int httplen = linecopylen - templen + 1;
  63.      
  64.     strncpy(http_version, httpstart, httplen);
  65.  
  66.     //compare http version strings
  67.     if (strcmp(http_version, "HTTP/1.1\r\n") != 0)
  68.     {  
  69.         error(505);
  70.         return false;
  71.     }
  72.    
  73.     //copy req target to a temp string
  74.     strcpy (tempquery, request_target);
  75.  
  76.    
  77.     //find the question mark + space and store the pointer to it
  78.     char* findqmark = strstr(tempquery, "? ");
  79.    
  80.     //if qmark and space exist, replace with nothing
  81.     if (findqmark != NULL)
  82.     {
  83.         strcpy(query, "");
  84.     }
  85.    
  86.     //find the query and store the pointer to it.
  87.     char* findq = strchr(request_target, 'q');
  88.    
  89.     //if q is found, copy that to query
  90.     if (findq != NULL)
  91.     {
  92.         strcpy(query, findq);
  93.     }
  94.    
  95.     //else copy entire req target to abs_path
  96.     else strcpy(abs_path, request_target);
  97.    
  98.     //free the dyn mem allocation
  99.     free (linecopy);
  100.     free (method);
  101.     free (request_target);
  102.     free (http_version);
  103.     free (tempquery);
  104.    
  105.     return true;
  106. }
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.

×