Advertisement
tkamiten

Pset6 functions

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