Guest User

AdiKhajuria-CS50-Pset6-server-query-issue

a guest
Nov 5th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. bool parse(const char* line, char* abs_path, char* query)
  2. {
  3. //check if the HTTP version in a request is 1.1
  4. char* httpVersion = strstr(line,"HTTP/1.1");
  5. if (httpVersion == NULL)
  6. {
  7. error (505);
  8. return false;
  9. }
  10.  
  11. char method[5];
  12. strncpy (method, line, 4);
  13. method[4] = '\0';
  14. if(strncmp(method, "GET ", 4) != 0)
  15. {
  16. error(405);
  17. return false;
  18. }
  19.  
  20. // Check if the request-target has a forward slash
  21. char* requestPath = strchr(line, '/');
  22. requestPath[strlen(requestPath) - 11] = '\0';
  23.  
  24. if (line[3] == ' ' && line[3 + 1] == '/')
  25. {
  26. strcpy(abs_path,requestPath);
  27. abs_path[strlen(requestPath)] = '\0';
  28. }
  29.  
  30. if (abs_path[0] != '/')
  31. {
  32. error(501);
  33. return false;
  34. }
  35.  
  36. char* queryString = "a";
  37. if (strchr(line, '?') != NULL)
  38. {
  39. printf("line has a query\n");
  40. queryString = strrchr(line,'?');
  41. printf("line's query:%s\n", queryString);
  42. }
  43.  
  44. //if the query string has a question mark, remove it
  45. if (strlen(queryString) >= 1 && queryString[0] == '?')
  46. {
  47. for(int i = 0; i < strlen(queryString); i++)
  48. {
  49. if (queryString[i] == '?')
  50. {
  51. query[i] = queryString[i+1];
  52. continue;
  53. }
  54. if (i + 1 < strlen(queryString))
  55. {
  56. query[i] = queryString[i+1];
  57. }
  58. if (i == strlen(queryString) - 1)
  59. {
  60. query[i] = '\0';
  61. }
  62. }
  63. query[strlen(queryString)] = '\0';
  64. printf("The query:%s\n", query);
  65. }
  66.  
  67. //check if the query string has something in it.
  68. if(strstr(abs_path, "?") != NULL && strlen(queryString) >= 1)
  69. {
  70. abs_path[strlen(abs_path) - (strlen(queryString))] = '\0';
  71. }
  72.  
  73. // Check if the request-target does not contain a speech mark
  74. if (strstr(requestPath,"\"") != NULL)
  75. {
  76. error(400);
  77. return false;
  78. }
  79.  
  80. if(strstr(abs_path,"cat.exe") != NULL)
  81. {
  82. error(501);
  83. return false;
  84. }
  85.  
  86. printf("abs_path:%s\n", abs_path);
  87. return true;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment