Advertisement
Guest User

PSet6-AdiKhajuria-Server.c-parse-2

a guest
Oct 29th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. bool parse(const char* line, char* abs_path, char* query)
  2. {
  3. char forwardSlash = '/';
  4. char* requestPath = strchr(line, forwardSlash);
  5.  
  6. char queryChar = '?';
  7. char* queryString = strrchr(line, queryChar);
  8.  
  9. unsigned int lineLength = strlen(line);
  10. query[0] = '\0';
  11.  
  12. //check if the HTTP version in a request is 1.1
  13. char* httpVersion = strstr(line,"HTTP/1.1");
  14. if (httpVersion == NULL)
  15. {
  16. error (505);
  17. return false;
  18. }
  19.  
  20. // iterates through line character by character
  21. for (int i = 0; i < lineLength; i++)
  22. {
  23. if (line[i] == forwardSlash)
  24. {
  25. strcpy(abs_path,requestPath);
  26. }
  27. if (line[i] == queryChar)
  28. {
  29. strcpy(query, queryString);
  30. }
  31. }
  32.  
  33. // Check if the request-target has a forward slash
  34. if (abs_path[0] != '/')
  35. {
  36. error(501);
  37. return false;
  38. }
  39.  
  40. // Check if the request-target does not contain a speech mark
  41. else if (strstr(line, "\"") != NULL )
  42. {
  43. error(400);
  44. return false;
  45. }
  46.  
  47. // Check if the request method is GET
  48. char* method = strstr(line,"GET");
  49.  
  50. if (strncmp(line, method,sizeof(3)) != 0 )
  51. {
  52. error (405);
  53. return false;
  54. }
  55.  
  56. error(501);
  57. return false;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement