Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- void error(const char *msg) {
- perror(msg);
- exit(1);
- }
- int main(int argc, char *argv[]) {
- if (argc < 2) {
- fprintf(stderr, "Usage: %s <port>\n", argv[0]);
- exit(1);
- }
- FILE *fptr;
- char response[8192];
- char html[8192];
- char xml[8192];
- char id[256];
- char val[256];
- char err[256];
- char field[256];
- char line[256];
- char *lines[256];
- int sockfd, newsockfd, portno, n;
- char buffer[1024];
- struct sockaddr_in serv_addr, cli_addr;
- socklen_t clilen;
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0) {
- error("Unable to open socket");
- }
- bzero((char *)&serv_addr, sizeof(serv_addr));
- portno = atoi(argv[1]);
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(portno);
- if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
- error("Unable to bind");
- }
- listen(sockfd, 5);
- clilen = sizeof(cli_addr);
- while (1) {
- newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
- if (newsockfd < 0) {
- error("Unable to accept connection");
- }
- bzero(buffer, sizeof(buffer));
- n = read(newsockfd, buffer, sizeof(buffer) - 1);
- if (n < 0) {
- error("Failed to read");
- }
- printf("Request:\n%s\n", buffer);
- if (strncmp(buffer, "GET / ", 6) == 0) {
- fptr = fopen("index.html", "r");
- if (fptr == NULL) {
- fprintf(stderr, "Error opening view file\n");
- exit(1);
- }
- fread(html, 1, sizeof(html), fptr);
- fclose(fptr);
- snprintf(response, sizeof(response),
- "HTTP/1.1 200 OK\r\n"
- "Content-Length: %zu\r\n"
- "Content-Type: text/html\r\n\r\n%s",
- strlen(html), html);
- n = write(newsockfd, response, strlen(response));
- if (n < 0) {
- error("Failed to write");
- }
- } else if (strncmp(buffer, "GET /getTasks ", 10) == 0) {
- fptr = fopen("data.xml", "r");
- if (fptr == NULL) {
- fprintf(stderr, "Error opening data file\n");
- exit(1);
- }
- fread(xml, 1, sizeof(xml), fptr);
- fclose(fptr);
- snprintf(response, sizeof(response),
- "HTTP/1.1 200 OK\r\n"
- "Content-Length: %zu\r\n"
- "Content-Type: text/xml\r\n\r\n%s",
- strlen(xml), xml);
- n = write(newsockfd, response, strlen(response));
- if (n < 0) {
- error("Failed to write");
- }
- } else if (strncmp(buffer, "POST /postTask", 14) == 0) {
- char *start = strstr(buffer, "\"task\":\"");
- if (start == NULL) {
- fprintf(stderr, "Bad post string");
- } else {
- start += 8;
- int i = 0;
- while (*start != '\0' && *start != '\"') {
- val[i++] = *start++;
- }
- val[i] = '\0';
- if (strcmp(val, "") == 0) {
- strcpy(err,
- "{\"error\":\"The field task is empty\", "
- "\"field\":\"taskInput\"}");
- snprintf(response, sizeof(response),
- "HTTP/1.1 400 Bad Request\r\n"
- "Content-Length: %zu\r\n"
- "Content-Type: application/json\r\n\r\n%s",
- strlen(err), err);
- printf("%s", err);
- n = write(newsockfd, response, strlen(response));
- if (n < 0) {
- error("Failed to write");
- }
- } else {
- fptr = fopen("data.xml", "r+");
- if (fptr == NULL) {
- fprintf(stderr, "Error opening data file\n");
- exit(1);
- }
- int newId = 0;
- while (fgets(line, sizeof(line), fptr) != NULL) {
- if (strstr(line, "<id>") != NULL) {
- char *idStart = strchr(line, '>') + 1;
- char *idEnd = strchr(line, '<');
- *idEnd = '\0';
- int currentId = atoi(idStart);
- if (currentId >= newId) {
- newId = currentId + 1;
- }
- }
- }
- fseek(fptr, -9, SEEK_END);
- fprintf(fptr,
- "\r\n <task>\r\n"
- " <id>%d</id>\r\n"
- " <value>%s</value>\r\n"
- " </task>\r\n</data>",
- newId, val);
- fclose(fptr);
- strcpy(err, "{\"success\": 1}");
- snprintf(response, sizeof(response),
- "HTTP/1.1 200 OK\r\n"
- "Content-Length: %zu\r\n"
- "Content-Type: application/json\r\n\r\n%s",
- strlen(err), err);
- n = write(newsockfd, response, strlen(response));
- if (n < 0) {
- error("Failed to write");
- }
- }
- }
- } else if (strncmp(buffer, "DELETE /delete ", 14) == 0) {
- char *start = strstr(buffer, "\"id\":\"");
- if (start == NULL) {
- fprintf(stderr, "Bad post string");
- } else {
- start += 6;
- int i = 0;
- while (*start != '\0' && *start != '\"') {
- id[i++] = *start++;
- }
- id[i] = '\0';
- start = strstr(buffer, "\"task\":\"");
- if (start == NULL) {
- fprintf(stderr, "Bad post string");
- } else {
- start += 8;
- int i = 0;
- while (*start != '\0' && *start != '\"') {
- val[i++] = *start++;
- }
- val[i] = '\0';
- fptr = fopen("data.xml", "r+");
- if (fptr == NULL) {
- fprintf(stderr, "Error opening data file\n");
- exit(1);
- }
- if (strcmp(val, "") == 0 || strcmp(id, "") == 0) {
- strcpy(err,
- "{\"error\":\"The field task is empty\", "
- "\"field\":\"taskInput\"}");
- snprintf(response, sizeof(response),
- "HTTP/1.1 400 Bad Request\r\n"
- "Content-Length: %zu\r\n"
- "Content-Type: application/json\r\n\r\n%s",
- strlen(err), err);
- printf("%s", err);
- n = write(newsockfd, response, strlen(response));
- if (n < 0) {
- error("Failed to write");
- } else {
- int lineIndex = 0;
- while (fgets(line, sizeof(line), fptr) != NULL) {
- lines[lineIndex] = strdup(line);
- lineIndex++;
- if (strstr(line, "<id>") != NULL) {
- char *idStart = strchr(line, '>') + 1;
- char *idEnd = strchr(line, '<');
- *idEnd = '\0';
- int currentId = atoi(idStart);
- if (currentId == atoi(id)) {
- lineIndex -= 2;
- fseek(fptr, 34 + strlen(val), SEEK_END);
- }
- }
- }
- int lineSize = sizeof(lines) / sizeof(lines[0]);
- printf("Line size:%i", lines[lineSize - 1]);
- }
- }
- }
- }
- }
- close(newsockfd);
- }
- close(sockfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment