SHARE
TWEET

Untitled

a guest May 15th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <string.h>
  10. #include <inttypes.h>
  11. #include <stdbool.h>
  12. #include <signal.h>
  13. #include <errno.h>
  14. #include <netdb.h>
  15.  
  16.  
  17. int main(int argc, char* argv[]) {
  18.  
  19.     char *hostname = argv[1];
  20.     char *path = argv[2];
  21.     signal(SIGPIPE, SIG_IGN);
  22.  
  23.     struct addrinfo addr_hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};
  24.     struct addrinfo *addr_result = NULL;
  25.     getaddrinfo(hostname, "http", &addr_hints, &addr_result);
  26.     int sock = socket(AF_INET, SOCK_STREAM, 0);
  27.     connect(sock, addr_result->ai_addr, addr_result->ai_addrlen);
  28.     char req[4096];
  29.     snprintf(req, sizeof(req),
  30.              "GET %s HTTP/1.1\n"
  31.              "Host: %s\n"
  32.              "COnnection: close\n"
  33.              "\n",
  34.              path, hostname);
  35.     write(sock, req, strnlen(req, sizeof(req)));
  36.     FILE *in = fdopen(sock, "r");
  37.     char minibuf[65536];
  38.     bool headers_completed = 0;
  39.     while (fgets(minibuf, sizeof(minibuf), in)) {
  40.         if (0 == strcmp(minibuf, "\n") || 0 == strcmp(minibuf, "\r\n")) {
  41.             headers_completed = 1;
  42.             continue;
  43.         }
  44.         if (headers_completed) {
  45.             printf("%s", minibuf);
  46.         }
  47.     }
  48.     fclose(in);
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top