Advertisement
danielhilst

httpfile.c

Oct 9th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <netinet/in.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10.  
  11. #define STR "Hello tcp world\n"        
  12. #define pexit(x)                                \
  13.         do {                                    \
  14.                 perror(x);                      \
  15.                 exit(EXIT_FAILURE);             \
  16.         } while (0)
  17.  
  18. static struct sockaddr_in saddr;
  19.  
  20. int main(int argc, char **argv)
  21. {
  22.         int sock, clisock;
  23.  
  24.         if (argc < 2) {
  25.                 printf("Usage: %s FILE_TO_SERV [PORT]", argv[0]);
  26.                 exit(EXIT_FAILURE);
  27.         }
  28.  
  29.         assert(argv[1]);
  30.  
  31.         sock = socket(AF_INET, SOCK_STREAM, 0);
  32.        
  33.         saddr.sin_family = AF_INET;
  34.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  35.         if (argc == 3)
  36.                 saddr.sin_port = htons(atoi(argv[2]));
  37.         else
  38.                 saddr.sin_port = htons(80);
  39.  
  40.         if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)))
  41.                 pexit("bind");
  42.  
  43.         if (listen(sock, 10))
  44.                 pexit("listen");
  45.  
  46.         for (;;) {
  47.                 FILE *fp;
  48.                 struct stat s;
  49. #define LBUFLEN 256
  50.                 char httpheader_buf[LBUFLEN];
  51.                 char linebuf[LBUFLEN];
  52.                 int filesiz;
  53.  
  54.                 if ((clisock = accept(sock, NULL, NULL)) == -1) {
  55.                         perror("accept");
  56.                         continue;
  57.                 }
  58.                 puts("New connection");
  59.  
  60.                 while (stat(argv[1], &s))
  61.                         sleep(1);
  62.                 puts("Sending file contents");
  63.                
  64.                 fp = fopen(argv[1], "r");
  65.                 if (!fp) {
  66.                         perror("fopen");
  67.                         continue;
  68.                 }
  69.                
  70.                 fseek(fp, 0L, SEEK_END);
  71.                 filesiz = ftell(fp);
  72.                 fseek(fp, 0L, SEEK_SET);
  73.  
  74.                 snprintf(httpheader_buf, LBUFLEN,
  75.                         "HTTP/1.1 200 OK\r\n"
  76.                         "Content-Type: text/html; charset=UTF-8\r\n"
  77.                         "Content-Length: %d\r\n"
  78.                         "Server: dummy\r\n"
  79.                         "\r\n", filesiz);
  80.  
  81.                 send(clisock, httpheader_buf, strlen(httpheader_buf), 0);
  82.  
  83.                 do {
  84.                         fgets(linebuf, LBUFLEN, fp);
  85.                         send(clisock, linebuf, strlen(linebuf), 0);
  86.                 } while (!feof(fp));
  87.  
  88.                 close(clisock);
  89.                 fclose(fp);
  90.         }
  91.  
  92.         return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement