Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <signal.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <stdlib.h>
- #include <zconf.h>
- #include <string.h>
- #include <stdbool.h>
- int main(int argc, char *argv[]) {
- char *host_name = 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(host_name, "http", &addr_hints, &addr_result);
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- if(0 != connect(sock, addr_result->ai_addr, addr_result->ai_addrlen)) {
- perror("connect");
- exit(1);
- }
- char request[4096];
- snprintf(request, sizeof(request),
- "GET %s HTTP/1.1\n"
- "Host: %s\n"
- "Connection: close\n"
- "\n",
- path, host_name);
- write(sock, request, strnlen(request, sizeof(request)));
- FILE* in = fdopen(sock, "r");
- char minibuff[65536];
- bool headers_completed = 0;
- while(fgets(minibuff, sizeof(minibuff), in)) {
- if(0==strcmp(minibuff, "\n") || 0==strcmp(minibuff, "\r\n")) {
- headers_completed = 1;
- continue;
- }
- if(headers_completed) {
- printf("%s", minibuff);
- }
- }
- fclose(in);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement