Guest User

Untitled

a guest
Apr 2nd, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9.  
  10. void error(const char *msg) {
  11. perror(msg);
  12. exit(1);
  13. }
  14.  
  15. int main(int argc, char *argv[]) {
  16. if (argc < 2) {
  17. fprintf(stderr, "Usage: %s <port>\n", argv[0]);
  18. exit(1);
  19. }
  20.  
  21. FILE *fptr;
  22. char response[8192];
  23. char html[8192];
  24. char xml[8192];
  25. char id[256];
  26. char val[256];
  27. char err[256];
  28. char field[256];
  29. char line[256];
  30. char *lines[256];
  31.  
  32. int sockfd, newsockfd, portno, n;
  33. char buffer[1024];
  34. struct sockaddr_in serv_addr, cli_addr;
  35. socklen_t clilen;
  36.  
  37. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  38. if (sockfd < 0) {
  39. error("Unable to open socket");
  40. }
  41.  
  42. bzero((char *)&serv_addr, sizeof(serv_addr));
  43. portno = atoi(argv[1]);
  44.  
  45. serv_addr.sin_family = AF_INET;
  46. serv_addr.sin_addr.s_addr = INADDR_ANY;
  47. serv_addr.sin_port = htons(portno);
  48.  
  49. if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  50. error("Unable to bind");
  51. }
  52.  
  53. listen(sockfd, 5);
  54.  
  55. clilen = sizeof(cli_addr);
  56.  
  57. while (1) {
  58. newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  59. if (newsockfd < 0) {
  60. error("Unable to accept connection");
  61. }
  62.  
  63. bzero(buffer, sizeof(buffer));
  64. n = read(newsockfd, buffer, sizeof(buffer) - 1);
  65.  
  66. if (n < 0) {
  67. error("Failed to read");
  68. }
  69.  
  70. printf("Request:\n%s\n", buffer);
  71.  
  72. if (strncmp(buffer, "GET / ", 6) == 0) {
  73. fptr = fopen("index.html", "r");
  74. if (fptr == NULL) {
  75. fprintf(stderr, "Error opening view file\n");
  76. exit(1);
  77. }
  78.  
  79. fread(html, 1, sizeof(html), fptr);
  80. fclose(fptr);
  81.  
  82. snprintf(response, sizeof(response),
  83. "HTTP/1.1 200 OK\r\n"
  84. "Content-Length: %zu\r\n"
  85. "Content-Type: text/html\r\n\r\n%s",
  86. strlen(html), html);
  87.  
  88. n = write(newsockfd, response, strlen(response));
  89. if (n < 0) {
  90. error("Failed to write");
  91. }
  92. } else if (strncmp(buffer, "GET /getTasks ", 10) == 0) {
  93. fptr = fopen("data.xml", "r");
  94. if (fptr == NULL) {
  95. fprintf(stderr, "Error opening data file\n");
  96. exit(1);
  97. }
  98.  
  99. fread(xml, 1, sizeof(xml), fptr);
  100. fclose(fptr);
  101.  
  102. snprintf(response, sizeof(response),
  103. "HTTP/1.1 200 OK\r\n"
  104. "Content-Length: %zu\r\n"
  105. "Content-Type: text/xml\r\n\r\n%s",
  106. strlen(xml), xml);
  107.  
  108. n = write(newsockfd, response, strlen(response));
  109. if (n < 0) {
  110. error("Failed to write");
  111. }
  112. } else if (strncmp(buffer, "POST /postTask", 14) == 0) {
  113. char *start = strstr(buffer, "\"task\":\"");
  114. if (start == NULL) {
  115. fprintf(stderr, "Bad post string");
  116. } else {
  117. start += 8;
  118. int i = 0;
  119. while (*start != '\0' && *start != '\"') {
  120. val[i++] = *start++;
  121. }
  122. val[i] = '\0';
  123. if (strcmp(val, "") == 0) {
  124. strcpy(err,
  125. "{\"error\":\"The field task is empty\", "
  126. "\"field\":\"taskInput\"}");
  127. snprintf(response, sizeof(response),
  128. "HTTP/1.1 400 Bad Request\r\n"
  129. "Content-Length: %zu\r\n"
  130. "Content-Type: application/json\r\n\r\n%s",
  131. strlen(err), err);
  132. printf("%s", err);
  133. n = write(newsockfd, response, strlen(response));
  134. if (n < 0) {
  135. error("Failed to write");
  136. }
  137. } else {
  138. fptr = fopen("data.xml", "r+");
  139. if (fptr == NULL) {
  140. fprintf(stderr, "Error opening data file\n");
  141. exit(1);
  142. }
  143.  
  144. int newId = 0;
  145. while (fgets(line, sizeof(line), fptr) != NULL) {
  146. if (strstr(line, "<id>") != NULL) {
  147. char *idStart = strchr(line, '>') + 1;
  148. char *idEnd = strchr(line, '<');
  149. *idEnd = '\0';
  150. int currentId = atoi(idStart);
  151. if (currentId >= newId) {
  152. newId = currentId + 1;
  153. }
  154. }
  155. }
  156.  
  157. fseek(fptr, -9, SEEK_END);
  158.  
  159. fprintf(fptr,
  160. "\r\n <task>\r\n"
  161. " <id>%d</id>\r\n"
  162. " <value>%s</value>\r\n"
  163. " </task>\r\n</data>",
  164. newId, val);
  165.  
  166. fclose(fptr);
  167.  
  168. strcpy(err, "{\"success\": 1}");
  169. snprintf(response, sizeof(response),
  170. "HTTP/1.1 200 OK\r\n"
  171. "Content-Length: %zu\r\n"
  172. "Content-Type: application/json\r\n\r\n%s",
  173. strlen(err), err);
  174.  
  175. n = write(newsockfd, response, strlen(response));
  176. if (n < 0) {
  177. error("Failed to write");
  178. }
  179. }
  180. }
  181. } else if (strncmp(buffer, "DELETE /delete ", 14) == 0) {
  182. char *start = strstr(buffer, "\"id\":\"");
  183. if (start == NULL) {
  184. fprintf(stderr, "Bad post string");
  185. } else {
  186. start += 6;
  187. int i = 0;
  188. while (*start != '\0' && *start != '\"') {
  189. id[i++] = *start++;
  190. }
  191. id[i] = '\0';
  192. start = strstr(buffer, "\"task\":\"");
  193. if (start == NULL) {
  194. fprintf(stderr, "Bad post string");
  195. } else {
  196. start += 8;
  197. int i = 0;
  198. while (*start != '\0' && *start != '\"') {
  199. val[i++] = *start++;
  200. }
  201. val[i] = '\0';
  202. fptr = fopen("data.xml", "r+");
  203. if (fptr == NULL) {
  204. fprintf(stderr, "Error opening data file\n");
  205. exit(1);
  206. }
  207. if (strcmp(val, "") == 0 || strcmp(id, "") == 0) {
  208. strcpy(err,
  209. "{\"error\":\"The field task is empty\", "
  210. "\"field\":\"taskInput\"}");
  211. snprintf(response, sizeof(response),
  212. "HTTP/1.1 400 Bad Request\r\n"
  213. "Content-Length: %zu\r\n"
  214. "Content-Type: application/json\r\n\r\n%s",
  215. strlen(err), err);
  216. printf("%s", err);
  217. n = write(newsockfd, response, strlen(response));
  218. if (n < 0) {
  219. error("Failed to write");
  220. } else {
  221. int lineIndex = 0;
  222. while (fgets(line, sizeof(line), fptr) != NULL) {
  223. lines[lineIndex] = strdup(line);
  224. lineIndex++;
  225. if (strstr(line, "<id>") != NULL) {
  226. char *idStart = strchr(line, '>') + 1;
  227. char *idEnd = strchr(line, '<');
  228. *idEnd = '\0';
  229. int currentId = atoi(idStart);
  230. if (currentId == atoi(id)) {
  231. lineIndex -= 2;
  232. fseek(fptr, 34 + strlen(val), SEEK_END);
  233. }
  234. }
  235. }
  236. int lineSize = sizeof(lines) / sizeof(lines[0]);
  237. printf("Line size:%i", lines[lineSize - 1]);
  238. }
  239. }
  240. }
  241. }
  242. }
  243.  
  244. close(newsockfd);
  245. }
  246.  
  247. close(sockfd);
  248. return 0;
  249. }
  250.  
Advertisement
Add Comment
Please, Sign In to add comment