Advertisement
tkamiten

pset6 functions 2

May 29th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. char* indexes(const char* path)
  2. {
  3. DIR *dir_ptr = opendir(path);
  4.  
  5.  
  6. while(readdir(dir_ptr) != NULL)
  7. {
  8. struct dirent* dir_f = readdir(dir_ptr);
  9.  
  10. if(strcmp(dir_f->d_name, "index.php") == 0)
  11. {
  12. char* path_p = malloc(strlen(path) + 1 + 9);
  13. strcpy(path_p, path);
  14. strcat(path_p, "index.php");
  15. return path_p;
  16. free(path_p);
  17. break;
  18. }
  19. else if(strcmp(dir_f->d_name, "index.html") == 0)
  20. {
  21. char* path_h = malloc(strlen(path) + 1 + 10);
  22. strcpy(path_h, path);
  23. strcat(path_h, "index.html");
  24. return path_h;
  25. free(path_h);
  26. break;
  27. }
  28. else
  29. break;
  30. }
  31. return NULL;
  32. }
  33. bool load(FILE* file, BYTE** content, size_t* length)
  34. {
  35. BYTE *buf = NULL;
  36. int counter = 0;
  37.  
  38. while(feof(file)==0)
  39. {
  40. fread(buf,sizeof(BYTE), 1, file);
  41. counter++;
  42. }
  43.  
  44. *length = counter;
  45.  
  46. // array for loading
  47. BYTE *b_array;
  48.  
  49. b_array = (BYTE *)malloc(sizeof(BYTE)*counter);
  50.  
  51. // set file start point
  52. fseek(file, 0, SEEK_SET);
  53.  
  54. *content = b_array;
  55.  
  56. int i = 0;
  57.  
  58. while(feof(file)==0)
  59. {
  60. fread(buf,sizeof(BYTE), 1, file);
  61. b_array[i] = *buf;
  62. i++;
  63. }
  64. return true;
  65. }
  66.  
  67. /**
  68. * Returns MIME type for supported extensions, else NULL.
  69. */
  70. const char* lookup(const char* path)
  71. {
  72. // char pointer to find dot
  73. char* dot_ptr;
  74.  
  75. // array to store file type extension
  76. char ext[6];
  77.  
  78. int i = 0;
  79.  
  80. dot_ptr = strrchr(path,'.');
  81.  
  82. do
  83. {
  84. ext[i] = *dot_ptr;
  85. i++;
  86. dot_ptr++;
  87. }while(*dot_ptr != '\0');
  88.  
  89. ext[i] = '\0';
  90.  
  91. if(strcasecmp(".css", ext) == 0)
  92. return "text/css";
  93. else if(strcasecmp(".html", ext) == 0)
  94. return "text/html";
  95. else if(strcasecmp(".gif", ext) == 0)
  96. return "image/gif";
  97. else if(strcasecmp(".ico", ext) == 0)
  98. return "image/x-icon";
  99. else if(strcasecmp(".jpg", ext) == 0)
  100. return "image/jpeg";
  101. else if(strcasecmp(".js", ext) == 0)
  102. return "text/javascript";
  103. else if(strcasecmp(".php", ext) == 0)
  104. return "text/x-php";
  105. else if(strcasecmp(".png", ext) == 0)
  106. return "image/png";
  107. else
  108. return NULL;
  109. }
  110.  
  111. /**
  112. * Parses a request-line, storing its absolute-path at abs_path
  113. * and its query string at query, both of which are assumed
  114. * to be at least of length LimitRequestLine + 1.
  115. */
  116. bool parse(const char* line, char* abs_path, char* query)
  117. {
  118. char* ptr_line;
  119.  
  120. // counter for space
  121. int sp_count = 0;
  122.  
  123. for(ptr_line = strchr(line,'G'); *ptr_line != 13; ptr_line++) // 13 = CR
  124. {
  125. if(*ptr_line == 32) // equals to space
  126. sp_count++;
  127. }
  128.  
  129. if(sp_count != 2)
  130. {
  131. error(400);
  132. return false;
  133. }
  134.  
  135. char get[] = "GET ";
  136.  
  137. // compare 4 characters between line and get
  138. if(strncmp(get, line, 4) != 0)
  139. {
  140. error(405);
  141. return false;
  142. }
  143.  
  144. // pointer for first space
  145. char* sp_ptr;
  146.  
  147. sp_ptr = strchr(line, 32); //32 = space
  148. sp_ptr++; // one character forward
  149.  
  150. if(*sp_ptr != '/')
  151. {
  152. error(501);
  153. return false;
  154. }
  155.  
  156. while(*sp_ptr != 32) // space
  157. {
  158. if(*sp_ptr == 34) // double quotation
  159. {
  160. error(400);
  161. return false;
  162. }
  163. sp_ptr++;
  164. }
  165.  
  166. // one character forward
  167. sp_ptr++;
  168.  
  169. char http[] = "HTTP/1.1";
  170.  
  171. if(strncmp(sp_ptr, http, 8) != 0)
  172. {
  173. error(505);
  174. return false;
  175. }
  176.  
  177. char ab_pass[LimitRequestLine+1];
  178. int i = 0;
  179. char* slash_ptr;
  180. slash_ptr = strchr(line, '/');
  181.  
  182. while(*slash_ptr != 32 && *slash_ptr != '?') // 32 = space
  183. {
  184. ab_pass[i] = *slash_ptr;
  185. i++;
  186. slash_ptr++;
  187. }
  188.  
  189. ab_pass[i] = '\0';
  190.  
  191. strcpy(abs_path, ab_pass);
  192.  
  193. //array to store query
  194. char qu[LimitRequestLine+1];
  195.  
  196. //pointer of ?
  197. char* quest_ptr = strchr(line, '?');
  198.  
  199. if(quest_ptr == NULL)
  200. qu[0] = '\0';
  201. else
  202. {
  203. quest_ptr++; // one character forward
  204. if(*quest_ptr == 32) //32 space
  205. qu[0] = '\0';
  206. else
  207. {
  208. int index = 0;
  209.  
  210. while(*quest_ptr != 32) // 32 space
  211. {
  212. qu[index] = *quest_ptr;
  213. quest_ptr++;
  214. index++;
  215. }
  216. qu[index] = '\0';
  217. }
  218. }
  219.  
  220. strcpy(query, qu);
  221.  
  222. return true;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement