Advertisement
Guest User

new pset6 code

a guest
Jul 3rd, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. const char* lookup(const char* path)
  2. {
  3. // TODO
  4. if (path == NULL)
  5. return NULL;
  6.  
  7.  
  8.  
  9. int pathLength = strlen(path);
  10. char* extension = calloc(1, pathLength);
  11.  
  12.  
  13.  
  14. extension = strchr(path, '.');
  15.  
  16.  
  17.  
  18. if(extension != NULL)
  19. {
  20. if (strcasecmp(extension, ".css") == 0)
  21. {
  22. char* ret = "text/css";
  23. return ret;
  24. }
  25. else if (strcasecmp(extension, ".html") == 0)
  26. {
  27. char* ret = "text/html";
  28. return ret;
  29. }
  30. else if (strcasecmp(extension, ".gif") == 0)
  31. {
  32. char* ret = "image/gif";
  33. return ret;
  34. }
  35. else if (strcasecmp(extension, ".ico") == 0)
  36. {
  37. char* ret = "image/x-icon";
  38. return ret;
  39. }
  40. else if (strcasecmp(extension, ".jpg") == 0)
  41. {
  42. char* ret = "image/jpeg";
  43. return ret;
  44. }
  45. else if (strcasecmp(extension, ".js") == 0)
  46. {
  47. char* ret = "text/javascript";
  48. return ret;
  49. }
  50. else if (strcasecmp(extension, ".php") == 0)
  51. {
  52. char* ret = "text/x-php";
  53. return ret;
  54. }
  55. else if (strcasecmp(extension, ".png") == 0)
  56. {
  57. char* ret = "image/png";
  58. return ret;
  59. }
  60. else
  61. {
  62. return NULL;
  63. }
  64. }
  65. else
  66. {
  67. return NULL;
  68. }
  69.  
  70.  
  71. }
  72.  
  73. /**
  74. * Parses a request-line, storing its absolute-path at abs_path
  75. * and its query string at query, both of which are assumed
  76. * to be at least of length LimitRequestLine + 1.
  77. */
  78. bool parse(const char* line, char* abs_path, char* query)
  79. {
  80. // TODO
  81.  
  82.  
  83. int lineLength = strlen(line);
  84. int sp = 0;
  85. int sp2 = 0;
  86.  
  87. char* method = calloc(1, lineLength);
  88. char* path = calloc(1, lineLength);
  89. char* http = calloc(1, lineLength);
  90.  
  91.  
  92. for(int i = 0, j = 0; i < lineLength; i++)
  93. {
  94. if (line[i] == ' ')
  95. {
  96. sp = i + 1;
  97. break;
  98. }
  99. if(line[i] != ' ' && j < 10)
  100. {
  101. method[j] = line[i];
  102. j++;
  103. }
  104. }
  105. if(strcmp(method, "GET") != 0)
  106. {
  107. error(405);
  108. return false;
  109. //printf("method is not GET, error 405\n");
  110. }
  111.  
  112.  
  113. for(int r = 0; sp < lineLength; sp++)
  114. {
  115. if (line[sp] == ' ')
  116. {
  117. sp2 = sp + 1;
  118. break;
  119. }
  120. if(line[sp] != ' ' && r < 50)
  121. {
  122. path[r] = line[sp];
  123.  
  124. r++;
  125. }
  126.  
  127. }
  128.  
  129.  
  130. if (path[0] != '/')
  131. {
  132. error(501);
  133. return false;
  134. //printf("path doesn't begin with fslash, error 501\n");
  135. }
  136.  
  137.  
  138. for(int c = 0; c < strlen(path); c++)
  139. {
  140. if (path[c] == '\"')
  141. {
  142. error(400);
  143. return false;
  144. //printf("path contains quotation mark, error 400\n");
  145.  
  146.  
  147. }
  148. }
  149. for(int w = 0; sp2 < lineLength; sp2++, w++)
  150. {
  151. if (line[sp2 != '\r'])
  152. {
  153. http[w] = line[sp2];
  154. }
  155. }
  156. if(strcmp(http, "HTTP/1.1") != 0)
  157. {
  158. error(505);
  159. return false;
  160. //printf("HTTP version is not 1.1, error 505\n");
  161. }
  162.  
  163.  
  164.  
  165. //lets extract this .
  166. //getting query:
  167.  
  168. char* qery = calloc(1, strlen(line));
  169.  
  170. qery = strchr(path,'?');
  171.  
  172. query = strchr(qery,'q');
  173.  
  174. char* p = strchr(path, '?');
  175.  
  176. *p = 0;
  177.  
  178. char* pPosition = strchr(path, '.');
  179. const char* lokup = lookup(path);
  180.  
  181. if(pPosition == NULL)
  182. {
  183. error(404);
  184. return false;
  185. //printf("the path doesn't contain a . error 404\n");
  186. }
  187. else if(lokup == NULL)
  188. {
  189. error(501);
  190. return false;
  191. //printf("the path extension is not valid here, error 501.\n");
  192. }
  193.  
  194. strcpy(abs_path, path);
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. return true;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement