Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <inttypes.h>
- #include <stdbool.h>
- #include <signal.h>
- #include <errno.h>
- #include <netdb.h>
- int main(int argc, char* argv[]) {
- char *hostname = argv[1];
- char *path = argv[2];
- signal(SIGPIPE, SIG_IGN);
- struct addrinfo addr_hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};
- struct addrinfo *addr_result = NULL;
- getaddrinfo(hostname, "http", &addr_hints, &addr_result);
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- connect(sock, addr_result->ai_addr, addr_result->ai_addrlen);
- char req[4096];
- snprintf(req, sizeof(req),
- "GET %s HTTP/1.1\n"
- "Host: %s\n"
- "COnnection: close\n"
- "\n",
- path, hostname);
- write(sock, req, strnlen(req, sizeof(req)));
- FILE *in = fdopen(sock, "r");
- char minibuf[65536];
- bool headers_completed = 0;
- while (fgets(minibuf, sizeof(minibuf), in)) {
- if (0 == strcmp(minibuf, "\n") || 0 == strcmp(minibuf, "\r\n")) {
- headers_completed = 1;
- continue;
- }
- if (headers_completed) {
- printf("%s", minibuf);
- }
- }
- fclose(in);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement