Downloader program for Wt library

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

//Set the server's address and port number
const char *proxy_host = "";
const int proxy_port = 8000;

//Downloaded file path
const char *filename = "xiuxiu.meitu.com";

// Define the download function
void download(void *cls, connection *c, const char *url, const char *content_type, const char *filename, off_t start, size_t len, struct MHD_Connection bus *cls) { // Get the file     size     FILE *fp = fopen(filename, "rb");     fseek(fp, start, SEEK_SET);     int file_size = ftell(fp);     fclose(fp);




    // Create a pipe and send the downloaded content to the server through the pipe
    int pipefd[2];
    if (pipe(pipefd) < 0) {         perror("pipe");         exit(EXIT_FAILURE);     }


    // Create a child process and send the downloaded content to the processing server through the child process
    pid_t pid = fork();
    if (pid < 0) {         perror("fork");         exit(EXIT_FAILURE);     }


    if (pid == 0) {
        // 子进程
        FILE *fp = fopen(filename, "rb");
        char *buffer = malloc(len + 1);
        if (buffer == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        int read_len;
        while ((read_len = fread(buffer, 1, len, fp)) > 0) {
            write(pipefd[1], buffer, read_len);
            buffer += read_len;
        }
        close(pipefd[1]);
        fclose(fp);
        wait(NULL);
        free(buffer);
        exit(EXIT_SUCCESS);
    } else {
        // 父进程
        char *command = "curl -s -o /dev/null -w '%{http_code}' -H 'Host: www.duoip.cn' -H 'Proxy-Authorization: Basic YWRtaW46cm9vbWlu' -X GET -H 'Connection: close' http://www.duoip.cn:8000/%s";
        char *proxy_command = (char *) malloc(strlen(command) + strlen(filename) + 1);
        if (proxy_command == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        strcpy(proxy_command, command);
        strcat(proxy_command, filename);
        int status;
        if (sysexits(status = system(proxy_command)) != 0) {
            perror("system");
            exit(EXIT_FAILURE);
        }
        close(pipefd[0]);
        close(pipefd[1]);
        wait(NULL);
        free(proxy_command);
    }
}

// 创建服务器
void start_server(void) {
    microhttpd_t *mh = MHD_create_server_from_socket(proxy_port, &download, NULL, 4096);
    if (mh == NULL) {
        perror("MHD_create_server_from_socket");
        exit(EXIT_FAILURE);
    }
    MHD_run(mh, NULL);
}

int main() {
    start_server();
    return 0;

This program first defines the server and port we want to use, and then defines a download task function, which will create a new download task and start downloading the specified URL. Once the download is complete, it saves the file and deletes the download task. The program also defines a download completion event handler, which will print a message and delete the task after the download task is completed.

In the main function, we first create a web server and a web session, and then handle the GET request from the client. If the requested URL parameter is "download", we create a new download task and start downloading the file. After the download is complete, we return a success response. If the requested URL parameter is not "download", we return an error response.

Note that this program is just a simple example, a real downloader program may need to handle more error conditions and exceptions, and may need to use more complex network libraries to handle the server. Additionally, this procedure also assumes that your server is running on the port and that your web application uses the Wt library. If your server runs on a different port, or your web application uses a different library, you may need to modify this procedure to suit your environment.

Guess you like

Origin blog.csdn.net/weixin_73725158/article/details/134070041