Guest User

main.c

a guest
Jun 2nd, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <arpa/inet.h>
  2. #include <errno.h>
  3. #include <netdb.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <unistd.h>
  9.  
  10. #define PORT "8080"
  11. #define BUFFSIZE 2048
  12. #define RESSIZE 4096
  13. #include "filehandle.h"
  14. #include "httpresponse.h"
  15.  
  16. char *get_file_path(const char *request) {
  17.     char *path = NULL;
  18.     char *start = strstr(request, "GET ");
  19.     if (start != NULL) {
  20.     start += 4; // Move past "GET "
  21.     char *end = strstr(start, " ");
  22.     if (end != NULL) {
  23.         int path_length = end - start;
  24.         path = (char *)malloc(path_length + 1);
  25.         strncpy(path, start, path_length);
  26.         path[path_length] = '\0';
  27.     }
  28.     }
  29.     return path;
  30. };
  31.  
  32. char *requestpath(int sockfd) {
  33.     char *buffer = (char *)malloc(BUFFSIZE);
  34.  
  35.     int bytes = read(sockfd, buffer, BUFFSIZE);
  36.     return buffer;
  37. };
  38.  
  39. int main(int argc, char *argv[]) {
  40.  
  41.     int status;
  42.     struct addrinfo hints, *res;
  43.     int sockfd;
  44.     int bindstatus;
  45.  
  46.     memset(&hints, 0, sizeof hints);
  47.     hints.ai_family = AF_INET;
  48.     hints.ai_socktype = SOCK_STREAM;
  49.  
  50.     status = getaddrinfo(NULL, PORT, &hints, &res);
  51.     if (status == -1) {
  52.     fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  53.     }
  54.  
  55.     sockfd = socket(res->ai_family, res->ai_socktype, 0);
  56.     if (sockfd == -1) {
  57.     fprintf(stderr, "socket error %s\n", strerror(sockfd));
  58.     }
  59.  
  60.     int opt = 1;
  61.     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
  62.  
  63.     bindstatus = bind(sockfd, res->ai_addr, res->ai_addrlen);
  64.     if (bindstatus == -1) {
  65.     fprintf(stderr, "bind error %s\n", strerror(bindstatus));
  66.     }
  67.  
  68.     freeaddrinfo(res);
  69.  
  70.     int l;
  71.     l = listen(sockfd, 10);
  72.     if (l != 0) {
  73.     fprintf(stderr, "LISTEN ERROR %s\n", strerror(l));
  74.     }
  75.  
  76.     while (1) {
  77.     struct sockaddr_in client_addr;
  78.     int newfd;
  79.     socklen_t theiraddr = sizeof client_addr;
  80.     newfd = accept(sockfd, (struct sockaddr *)&client_addr, &theiraddr);
  81.  
  82.     if (newfd == -1) {
  83.         fprintf(stderr, "accept error %s\n", strerror(errno));
  84.     } else {
  85.         char str[INET_ADDRSTRLEN];
  86.         inet_ntop(client_addr.sin_family, (struct inaddr *)&client_addr.sin_addr,
  87.             str, INET_ADDRSTRLEN);
  88.         printf("connection from %s\n", str);
  89.     }
  90.  
  91.     char *request = requestpath(newfd);
  92.     char *filepath = get_file_path(request);
  93.     printf("%s\n", filepath);
  94.     free(request);
  95.  
  96.     char *response = NULL;
  97.  
  98.     if (strcmp(filepath, "/") == 0 || strcmp(filepath, "/index.html") == 0) {
  99.         response = content("index.html");
  100.  
  101.         char *http_response = (char *) malloc(RESSIZE);
  102.         char *date = get_date_for_server();
  103.         int content_length = strlen(response);
  104.         int len,bytes_sent;
  105.         int n = snprintf(http_response,RESSIZE,"HTTP/1.1 200 OK\r\nAcess-Control-Allow-Origin: *\r\nConnection: Keep-Alive\r\nDate: %s\r\nContent-Type: %s\r\nContent-Length: %llu\r\n\n%s",date,"text/html",content_length,response);
  106.         len = strlen(http_response);
  107.         bytes_sent = send(newfd,http_response,len,0);
  108.         free(date);
  109.         free(http_response);
  110.  
  111.         shutdown(newfd, SHUT_WR);
  112.     }
  113.     else if(strcmp(filepath,"/image.jpg") == 0){
  114.         char http_header[256];
  115.         char *date = get_date_for_server();
  116.  
  117.         struct file_data *fdata = NULL;
  118.         fdata = read_file("image.jpg");
  119.         if(fdata == NULL)
  120.             fprintf(stderr,"Error opening binary file");
  121.    
  122.         uint8_t buff[fdata->length];
  123.         FILE *wbf = fopen("test.jpg","wb");
  124.         fwrite(buff,fdata->length,1,wbf);
  125.         fclose(wbf);
  126.         snprintf(http_header,256,"HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: %llu\r\n",fdata->length);
  127.         send(newfd,http_header,strlen(http_header),0);
  128.         send(newfd,fdata->data,fdata->length,0);
  129.         printf("length is %ld\n",fdata->length);
  130.         shutdown(newfd, SHUT_WR);
  131.         fdata = NULL;
  132.  
  133.     }
  134.     else
  135.         shutdown(newfd, SHUT_WR);
  136.     }
  137.     close(sockfd);
  138.     return 0;
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment