Advertisement
DragonOsman

parse() function in server.c

Nov 29th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. bool parse(const char* line, char* abs_path, char* query)
  2. {
  3.     if (line[0] != 'G' && line[1] != 'E' && line[2] != 'T' && line[3] != ' ')
  4.     {
  5.         error(405);
  6.         return false;
  7.     }
  8.     if (line[3] != ' ')
  9.     {
  10.         error(405);
  11.         return false;
  12.     }
  13.     else if (line[4] != '/')
  14.     {
  15.         error(501);
  16.         return false;
  17.     }
  18.    
  19.     char* temp_path = strchr(line, '/');
  20.     int index = 0;
  21.     for (int length = strlen(temp_path); index < length; index++)
  22.     {
  23.         if (temp_path[index] == ' ')
  24.         {
  25.             break;
  26.         }
  27.     }
  28.     strncpy(abs_path, temp_path, index);
  29.     abs_path[index] = '\0';
  30.     for (int i = 0, n = strlen(abs_path); i < n; i++)
  31.     {
  32.         if (abs_path[i] == '"')
  33.         {
  34.             error(400);
  35.             return false;
  36.         }
  37.         if (abs_path[i] == '?')
  38.         {
  39.             return false;
  40.         }
  41.     }
  42.    
  43.     char* temp_query = strchr(line, '?');
  44.     if (temp_query != NULL)
  45.     {
  46.         index = 0;
  47.         for (int length = strlen(temp_query); index < length; index++)
  48.         {
  49.             if (temp_query[index] == ' ')
  50.             {
  51.                 break;
  52.             }
  53.         }
  54.         strncpy(query, temp_query, index);
  55.     }
  56.    
  57.     char* version = strstr(line, "HTTP/1.1");
  58.     if (version == NULL)
  59.     {
  60.         error(505);
  61.         return false;
  62.     }
  63.     else if (line[index + 1] == 'H' && line[index + 2] == 'T' && line[index + 3] == 'T' && line[index + 4] == 'P' &&
  64.              line[index + 5] == '/' && line[index + 6] == '1' && line[index + 7] == '.' && line[index + 8] == '0')
  65.     {
  66.         error(505);
  67.         return false;
  68.     }
  69.    
  70.     return true;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement