Guest User

Untitled

a guest
Jul 11th, 2016
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //function to concatenate strings adapted from : http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c
  2. char* concat(const char *s1, char *s2)
  3. {
  4. char *result = malloc(strlen(s1)+strlen(s2)+1);
  5. if (result != NULL)
  6. {
  7. return NULL;
  8. }
  9. strcpy(result, s1);
  10. strcat(result, s2);
  11. return result;
  12. }
  13.  
  14. char* indexes(const char* path)
  15. {
  16. char* pathphp = concat(path, "index.php");
  17. char* pathhtml = concat(path, "index.html");
  18.  
  19.  
  20.  
  21. if (access(pathphp, F_OK) != -1)
  22. {
  23. return pathphp;
  24. }
  25. else if (access(pathphp, F_OK) != -1)
  26. {
  27. return pathhtml;
  28. }
  29. else
  30. {
  31. return NULL;
  32. }
  33. free(pathphp);
  34. free(pathhtml);
  35.  
  36.  
  37. }
  38.  
  39.  
  40. bool load(FILE* file, BYTE** content, size_t* length)
  41. {
  42. // TODO
  43. *content = NULL;
  44. *length = 0;
  45. if (file == NULL)
  46. {
  47. return false;
  48. }
  49.  
  50. char *charac = calloc(1, sizeof(char));
  51.  
  52.  
  53.  
  54. int i = 0;
  55. int c = 0;
  56. do
  57. {
  58. charac = realloc(charac, sizeof(char) + (sizeof(char) * i));
  59. c = fgetc(file);
  60. charac[i] = c;
  61. length++;
  62. }while(charac[i] != EOF);
  63.  
  64. *content = charac;
  65.  
  66.  
  67.  
  68. return true;
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
  75. /**
  76. * Returns MIME type for supported extensions, else NULL.
  77. */
  78. const char* lookup(const char* path)
  79. {
  80. // TODO
  81. if (path == NULL)
  82. {
  83. return NULL;
  84. }
  85.  
  86.  
  87.  
  88. char* extension = strchr(path, '.');
  89.  
  90.  
  91. if(extension != NULL)
  92. {
  93. if (strcasecmp(extension, ".css") == 0)
  94. {
  95. char* ret = "text/css";
  96. return ret;
  97. }
  98. else if (strcasecmp(extension, ".html") == 0)
  99. {
  100. char* ret = "text/html";
  101. return ret;
  102. }
  103. else if (strcasecmp(extension, ".gif") == 0)
  104. {
  105. char* ret = "image/gif";
  106. return ret;
  107. }
  108. else if (strcasecmp(extension, ".ico") == 0)
  109. {
  110. char* ret = "image/x-icon";
  111. return ret;
  112. }
  113. else if (strcasecmp(extension, ".jpg") == 0)
  114. {
  115. char* ret = "image/jpeg";
  116. return ret;
  117. }
  118. else if (strcasecmp(extension, ".js") == 0)
  119. {
  120. char* ret = "text/javascript";
  121. return ret;
  122. }
  123. else if (strcasecmp(extension, ".php") == 0)
  124. {
  125. char* ret = "text/x-php";
  126. return ret;
  127. }
  128. else if (strcasecmp(extension, ".png") == 0)
  129. {
  130. char* ret = "image/png";
  131. return ret;
  132. }
  133. else
  134. {
  135. return NULL;
  136. }
  137. }
  138. else
  139. {
  140. return NULL;
  141. }
  142.  
  143.  
  144. }
  145.  
  146. /**
  147. * Parses a request-line, storing its absolute-path at abs_path
  148. * and its query string at query, both of which are assumed
  149. * to be at least of length LimitRequestLine + 1.
  150. */
  151. bool parse(const char* line, char* abs_path, char* query)
  152. {
  153. // TODO
  154.  
  155.  
  156. int lineLength = strlen(line);
  157. int sp = 0;
  158. int sp2 = 0;
  159.  
  160. char* method = calloc(1, lineLength);
  161. char* path = calloc(1, lineLength);
  162. char* http = calloc(1, lineLength);
  163. //char* query2 = calloc(1, lineLength);
  164.  
  165. for(int i = 0, j = 0; i < lineLength; i++)
  166. {
  167. if (line[i] == ' ')
  168. {
  169. sp = i + 1;
  170. break;
  171. }
  172. if(line[i] != ' ' && j < lineLength)
  173. {
  174. method[j] = line[i];
  175. j++;
  176. }
  177. }
  178. if(strcmp(method, "GET") != 0)
  179. {
  180. error(405);
  181. return false;
  182. //printf("method is not GET, error 405\n");
  183. }
  184.  
  185.  
  186. for(int r = 0; sp < lineLength; sp++)
  187. {
  188. if (line[sp] == ' ')
  189. {
  190. sp2 = sp + 1;
  191. break;
  192. }
  193. if(line[sp] != ' ' && r < lineLength)
  194. {
  195. path[r] = line[sp];
  196.  
  197. r++;
  198. }
  199.  
  200. }
  201.  
  202.  
  203. if (path[0] != '/')
  204. {
  205. error(501);
  206. return false;
  207. //printf("path doesn't begin with fslash, error 501\n");
  208. }
  209.  
  210.  
  211. for(int c = 0; c < strlen(path); c++)
  212. {
  213. if (path[c] == '\"')
  214. {
  215. error(400);
  216. return false;
  217. //printf("path contains quotation mark, error 400\n");
  218.  
  219.  
  220. }
  221. }
  222. for(int w = 0; sp2 < lineLength; sp2++, w++)
  223. {
  224. if (line[sp2] != '\r')
  225. {
  226. http[w] = line[sp2];
  227. }
  228. }
  229. if(strcmp(http, "HTTP/1.1") != 0)
  230. {
  231. error(505);
  232. return false;
  233. //printf("HTTP version is not 1.1, error 505\n");
  234. }
  235.  
  236.  
  237.  
  238. //lets extract this .
  239. //getting query:
  240.  
  241. char* qery = calloc(1, strlen(line));
  242.  
  243. if(strchr(path,'?') != NULL)
  244. {
  245. qery = strchr(path,'?');
  246.  
  247. *query = *strchr(qery,qery[1]);
  248.  
  249. char* p = strchr(path, '?');
  250.  
  251. *p = 0;
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. }
  259. strcpy(abs_path, path);
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. return true;
  267. }
RAW Paste Data