Advertisement
Guest User

practice code

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