Advertisement
danielhilst

http-get.c

May 9th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. /**
  2.  * @brief Give an url on format http://hostname/file or https://hostname/file
  3.  *        retrieve such file with a get. File is not saved.
  4.  * @author Daniel Hilst Selli <danielhilst@gmail.com>
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <netdb.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15.  
  16. #define pexit(x)                                \
  17.         do {                                    \
  18.                 perror(x);                      \
  19.                 exit(EXIT_FAILURE);             \
  20.         } while (0)
  21.  
  22. static char buf[BUFSIZ];
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.         int sock;
  27.         int error;
  28.         int n = 3;
  29.         int bytes = 0;
  30.         struct addrinfo *resaddr;
  31.         char *service;
  32.         char *hostname;
  33.         char *file;
  34.         char *ptr;
  35.  
  36.         if (argc != 2) {
  37.                 fprintf(stderr, "Usage: %s {http|https}://HOSTNAME[/FILE]\n", argv[0]);
  38.                 exit(EXIT_FAILURE);
  39.         }
  40.        
  41.         ptr = strchr(argv[1], ':');
  42.         if (!ptr) {
  43.                 fprintf(stderr, "ERROR: Bad Service\n");
  44.                 fprintf(stderr, "Usage: %s {http|https}://HOSTNAME[/FILE]\n", argv[0]);
  45.                 exit(EXIT_FAILURE);
  46.         }
  47.  
  48.         *ptr++ = '\0';          /* replace `://' to `\0\0\0' */
  49.         *ptr++ = '\0';          /* terminating service string */
  50.         *ptr++ = '\0';
  51.         service = argv[1];
  52.         printf("Service => %s\n", service);
  53.  
  54.         hostname = ptr;
  55.         ptr = strchr(ptr, '/');
  56.         if (ptr)
  57.                 *ptr++ = '\0';          /* replace `/' to `\0' terminating hostname string */
  58.  
  59.         printf("Hostname => %s\n", hostname);
  60.  
  61.         file = ptr ? ptr : "";
  62.         printf("File => %s\n", file);
  63.  
  64.         error = getaddrinfo(hostname, service, NULL, &resaddr);
  65.         if (error) {
  66.                 fprintf(stderr, "ERROR: %s\n", gai_strerror(error));
  67.                 exit(EXIT_FAILURE);
  68.         }
  69.  
  70.         printf("IP Address => %s\n", inet_ntoa(((struct sockaddr_in *)resaddr->ai_addr)->sin_addr));
  71.  
  72.         /* create socket */
  73.         sock = socket(resaddr->ai_family, resaddr->ai_socktype, resaddr->ai_protocol);
  74.         if (sock == -1)
  75.                 pexit("socket");
  76.  
  77.         error = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(int));
  78.  
  79.         error = connect(sock, resaddr->ai_addr, resaddr->ai_addrlen);
  80.         if (error)
  81.                 pexit("connect");
  82.         puts("Connected!");
  83.  
  84.         freeaddrinfo(resaddr);  /* free addrinfo linked list */
  85.  
  86.         snprintf(buf, BUFSIZ, "GET /%s\r\n", file);
  87.         n = send(sock, buf, strlen(buf), MSG_WAITALL);
  88.         if (n < 0)
  89.                 perror("send");
  90.         printf("%s\n", buf);
  91.  
  92.         while ((n = recv(sock, buf, BUFSIZ, 0)) > 0) {
  93.                 bytes += n;
  94.                 printf("%d bytes transferred\n", bytes);
  95.                 /* printf("%.*s", n, buf); */
  96.         }
  97.  
  98.         if (n == -1)
  99.                 pexit("recv");
  100.        
  101.         return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement