Advertisement
Guest User

Untitled

a guest
May 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netdb.h>
  6. #include <stdlib.h>
  7. #include <zconf.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10.  
  11. int main(int argc, char *argv[]) {
  12. char *host_name = argv[1];
  13. char *path = argv[2];
  14. signal(SIGPIPE, SIG_IGN);
  15. struct addrinfo addr_hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};
  16. struct addrinfo *addr_result = NULL;
  17. getaddrinfo(host_name, "http", &addr_hints, &addr_result);
  18. int sock = socket(AF_INET, SOCK_STREAM, 0);
  19. if(0 != connect(sock, addr_result->ai_addr, addr_result->ai_addrlen)) {
  20. perror("connect");
  21. exit(1);
  22. }
  23. char request[4096];
  24. snprintf(request, sizeof(request),
  25. "GET %s HTTP/1.1\n"
  26. "Host: %s\n"
  27. "Connection: close\n"
  28. "\n",
  29. path, host_name);
  30. write(sock, request, strnlen(request, sizeof(request)));
  31. FILE* in = fdopen(sock, "r");
  32. char minibuff[65536];
  33. bool headers_completed = 0;
  34. while(fgets(minibuff, sizeof(minibuff), in)) {
  35. if(0==strcmp(minibuff, "\n") || 0==strcmp(minibuff, "\r\n")) {
  36. headers_completed = 1;
  37. continue;
  38. }
  39. if(headers_completed) {
  40. printf("%s", minibuff);
  41. }
  42. }
  43. fclose(in);
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement