Advertisement
ProjectFi

Offending code, specifically

Jun 21st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. bool parse(const char* line, char* abs_path, char* query)
  2.     {
  3.     // declaring arrays for holding all these strings (and feels)
  4.     char abpathholder[LimitRequestLine + 1];
  5.     char queryholder[LimitRequestLine + 1];
  6.     char httpholder[9];
  7.     memset(abpathholder, 0, LimitRequestLine + 1);
  8.     memset(queryholder, 0, LimitRequestLine + 1);
  9.     memset(httpholder, 0, 9);
  10.     // getting the start of absolut path and the start of HTTP version - 1  
  11.     char* abpathstart = strchr(line, '/');
  12.     char* lastspace = strrchr(line, ' ');
  13.     char* querystart = strchr(line, '?');
  14.     // setting up an int to see where the path and query ends
  15.     int pathend;
  16.     int queryend;
  17.     // checks if there is a query and sets difference in strings accordingly, also gets the thing
  18.     if (querystart == NULL)
  19.     {
  20.         pathend = strlen(lastspace) - strlen(abpathstart);
  21.         queryholder[0] = '\0';
  22.     }
  23.     else
  24.     {
  25.         pathend = strlen(abpathstart) - strlen(querystart);
  26.         queryend = strlen(querystart) - strlen(lastspace) - 1;
  27.         for ( int i = 0; i < queryend; i++)
  28.         {
  29.             queryholder[i] = querystart[i + 1];
  30.         }
  31.     }
  32.     // copies accurate number of chars for abpath
  33.     for (int i = 0; i < pathend; i++)
  34.     {
  35.         abpathholder[i] = abpathstart[i];
  36.     }
  37.     //for (int i = 0; i < strlen(abpathholder); i++)
  38.     //    printf("This is in abpathholder: %c\n", abpathholder[i]);
  39.    
  40.     for (int i = 1; i < strlen(lastspace); i++)
  41.     {
  42.         if (lastspace[i] == '\r')
  43.         {
  44.             break;
  45.         }
  46.         else
  47.         {
  48.             httpholder[i - 1] = lastspace[i];
  49.         }
  50.     }
  51.     //for (int i = 0; i < strlen(httpholder); i++)
  52.     //    printf("This is in httpholder: %c\n", httpholder[i]);
  53.     // error checking begins here
  54.     // checking simply if line starts with GET
  55.     if (strncmp(line, "GET ", 4) != 0)
  56.     {
  57.         error(405);
  58.         return false;
  59.     }
  60.     // cheking for any " in line
  61.     for (int i = 0; i < strlen(line); i++)
  62.     {
  63.         if (line[i] == '"')
  64.         {
  65.             error(400);
  66.             return false;
  67.         }
  68.     }
  69.     // checking if request-target really begiins with a /
  70.     if (abpathholder[0] != '/' || abpathholder[0] == 47)
  71.     {
  72.         error(501);
  73.         return false;
  74.     }
  75.     // checking if HTTP version is correct
  76.     if (strcasecmp(httpholder, "HTTP/1.0") == true)
  77.     {
  78.         error(505);
  79.         return false;
  80.     }
  81.     abs_path = abpathholder;
  82.     query = queryholder;
  83.     for (int i = 0; i < strlen(abs_path); i++)
  84.         printf("This is in abs_path: %c\n", abs_path[i]);
  85.     for (int i = 0; i < strlen(query); i++)
  86.         printf("This is in query: %c\n", query[i]);
  87.     printf("Reached true!\n");
  88.     return true;
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement