HTTP File Downloader for Linux and Windows in C | Source Code


A member in HackForums by the handle Jakash3 has posted a source code on how to download files from the Internet that can be compiled in both Linux and Windows.  Another great feature is that it supports IPv6.

#if defined(_WIN32)
#include "WinSock2.h"
#include "Ws2tcpip.h"
#pragma comment(lib,"ws2_32.lib")
#elif defined(linux)
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#else
#error Mac detected.
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

void help() {
    puts(
        "HTTP File Downloader - by Jakash3\n"
        "Arguments: FILE_URL OUTPUT_FILE\n"
    );
    exit(1);
}

void fail(const char* format, ...) {
    va_list v;
    va_start(v, format);
    vprintf(format, v);
    exit(1);
}

int main(int argc, char** argv) {
    if (argc != 3) help();
#if defined(_WIN32)
    struct WSAData* wd = (struct WSAData*)malloc(sizeof(struct WSAData));
    if (WSAStartup(MAKEWORD(2, 0), wd))
        fail("Failed to initialize Winsock API.\n");
    free(wd);
    SOCKET sock;
#elif defined(linux)
    int sock;
#endif
    char c;
    int i, j;
    FILE* f;
    char* host = argv[1];
    struct addrinfo* ai;
    struct addrinfo hints;
    char buf[512];
    if (!(f = fopen(argv[2], "w"))) fail("Error. Failed to open: %s", argv[2]);
    if (strstr(argv[1], "http://") == argv[1]) host += 7;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    sprintf(buf, "GET %s HTTP/1.1\r\n", argv[1]);
    *strchr(host, '/') = '\0';
    if (i = getaddrinfo(host, "80", &hints, &ai)) fail("Error. %s\n", gai_strerror(i));
    sprintf(buf + strlen(buf), "Host: %s\r\n\r\n", host);
    sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (connect(sock, ai->ai_addr, ai->ai_addrlen))
        fail("Error. Failed to connect to host.\n");
    freeaddrinfo(ai);
    i = send(sock, buf, strlen(buf), 0);
    if (i < strlen(buf) || i == -1) fail("Error. Failed to send GET request!\n");
    while (strcmp(buf, "\r\n")) {
        for (i = 0; strcmp(buf + i - 2, "\r\n"); i++) { recv(sock, buf + i, 1, 0); buf[i + 1] = '\0'; }
        if (strstr(buf, "HTTP/") == buf) {
            puts(strchr(buf, ' ') + 1);
            if (strcmp(strchr(buf, ' ') + 1, "200 OK\r\n")) exit(1);
        }
        if (strstr(buf, "Content-Length:") == buf) {
            *strchr(buf, '\r') = '\0';
            j = atoi(strchr(buf, ' ') + 1);
        }
    }
    for (i = 0; i < j; i++) { recv(sock, &c, 1, 0); fputc(c, f); }
    fclose(f);
#if defined(_WIN32)
    closesocket(sock);
    WSACleanup();
#elif defined(linux)
    close(sock);
#endif
    return 0;
}

Source: [Win32/*nix] HTTP File Downloader

Advertisements

2 Responses to “HTTP File Downloader for Linux and Windows in C | Source Code”

  1. jakash3 Says:

    wowzers. Thanks for your support.

    • genesisdatabase Says:

      Your welcome.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: