Advertisement
Guest User

Untitled

a guest
Dec 28th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <netdb.h>
  6. #include <time.h>
  7. #include <sys/socket.h>
  8. #include <unistd.h>
  9. #include <arpa/inet.h>
  10.  
  11.  
  12.  
  13. #define SERVER_PORT "8000"
  14. #define MAX_CONNECTION 1000
  15.  
  16. typedef enum
  17. {
  18. eHTTP_UNKNOWN = 0
  19.  
  20. ,eHTTP_POST
  21. }eHTTPMethod;
  22.  
  23. typedef struct
  24. {
  25. eHTTPMethod type;
  26. char path[255];
  27. char body[7000];
  28. }sHTTPHeader;
  29.  
  30. void *get_client_addr(struct sockaddr *);
  31. int create_socket(const char *);
  32. void http_request(int);
  33. void parse_http_request(const char*, sHTTPHeader *);
  34.  
  35.  
  36. int main()
  37. {
  38. int sock;
  39.  
  40. sock = create_socket(SERVER_PORT);
  41. if(sock < 0)
  42. {
  43. fprintf(stderr, "error create socket\n");
  44. return -1;
  45. }
  46.  
  47. printf("server created!\n");
  48.  
  49. struct sockaddr_storage client_addr;
  50. int client_d;
  51. //char client_ip
  52. while(1)
  53. {
  54. socklen_t s_size = sizeof(client_addr);
  55. client_d = accept(sock, (struct sockaddr*)&client_addr, &s_size);
  56.  
  57. if(client_d == -1)
  58. {
  59. fprintf(stderr, "error accept\n");
  60. return -1;
  61. }
  62.  
  63. char ip[INET6_ADDRSTRLEN];
  64. inet_ntop(client_addr.ss_family, get_client_addr((struct sockaddr *)&client_addr), ip, sizeof ip);
  65. printf("server: got connection from %s\n", ip);
  66.  
  67. // read
  68. http_request(client_d);
  69.  
  70. close(client_d);
  71. }
  72.  
  73. return 0;
  74. }
  75.  
  76.  
  77. void *get_client_addr(struct sockaddr *sa)
  78. {
  79. if (sa->sa_family == AF_INET)
  80. {
  81. return &(((struct sockaddr_in*)sa)->sin_addr);
  82. }
  83.  
  84. return &(((struct sockaddr_in6*)sa)->sin6_addr);
  85. }
  86.  
  87. int create_socket(const char *apstrPort)
  88. {
  89. struct addrinfo hints;
  90. struct addrinfo *servinfo;
  91. struct addrinfo *p;
  92.  
  93. memset(&hints, 0, sizeof(hints));
  94.  
  95. // IPv4 or IPv6
  96. hints.ai_family = AF_UNSPEC;
  97. hints.ai_socktype = SOCK_STREAM;
  98. hints.ai_flags = AI_PASSIVE;
  99.  
  100. int r = getaddrinfo(NULL, apstrPort, &hints, &servinfo);
  101. if( r != 0)
  102. {
  103. fprintf(stderr, "error getaddrinfo()\n");
  104. return -1;
  105. }
  106.  
  107. int sock;
  108. int yes;
  109. for(p = servinfo; p != NULL; p = p->ai_next)
  110. {
  111. sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
  112. if(sock == -1)
  113. continue;
  114.  
  115. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  116. {
  117. fprintf(stderr, "error setsockopt\n");
  118. close(sock);
  119. freeaddrinfo(servinfo); // all done with this structure
  120. return -2;
  121. }
  122.  
  123. if(bind(sock, p->ai_addr, p->ai_addrlen) == -1)
  124. {
  125. close(sock);
  126. continue;
  127. }
  128. break;
  129. }
  130.  
  131. freeaddrinfo(servinfo); // all done with this structure
  132.  
  133. if(p == NULL)
  134. {
  135. fprintf(stderr, "failed to find address\n");
  136. return -3;
  137. }
  138.  
  139. if(listen(sock, MAX_CONNECTION) == -1)
  140. {
  141. fprintf(stderr, "error listen\n");
  142. return -4;
  143. }
  144.  
  145. return sock;
  146. }
  147.  
  148.  
  149. void http_request(int aSock)
  150. {
  151. const int request_buffer_size = 7000;
  152. char request[request_buffer_size];
  153.  
  154. int bytes_recvd = recv(aSock, request, request_buffer_size - 1, 0);
  155.  
  156. if (bytes_recvd < 0)
  157. {
  158. fprintf(stderr, "error recv\n");
  159. return;
  160. }
  161. request[bytes_recvd] = '\0';
  162.  
  163. printf("request:\n%s\n",request);
  164.  
  165. sHTTPHeader req;
  166. parse_http_request(request, &req);
  167. printf("%s",req.body);
  168. }
  169.  
  170. void parse_http_request(const char *apstrRequest, sHTTPHeader *apHeader) {
  171. int type_length = 0;
  172. char type[255] = {0};
  173. int index = 0;
  174.  
  175. apHeader->type = eHTTP_UNKNOWN;
  176.  
  177. sscanf(&apstrRequest[index], "%s", type);
  178. type_length = strlen(type);
  179.  
  180. if (type_length == 4) {
  181. if (type[0] == 'P' && type[1] == 'O' && type[2] == 'S' && type[3] == 'T') {
  182. apHeader->type = eHTTP_POST;
  183. index += type_length + 1;
  184. sscanf(&apstrRequest[index], "%s", apHeader->path);
  185. int k;
  186. for (k = index; k < 6500; k++) {
  187. if (apstrRequest[k] == '{') {
  188. break;
  189. }
  190. }
  191. for (int i = k, s = 0; i < 650; i++, s++) {
  192. apHeader->body[s] = apstrRequest[i];
  193.  
  194. }
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement