• C++语言代码示例


    #include
    #include
    #include
    #include

    #define MAX_URL_LEN 256

    typedef struct {
        char *url;
        char *filename;
    } url_info;

    void parse_url(const char *url, url_info *info) {
        info->url = url;
        info->filename = (char*)malloc(strlen(url) + 5); // allocate 5 bytes for ".mp4"
    }

    void handle_request(struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void *user_obj) {
        url_info *info = (url_info*)user_obj;
        if (strcmp(method, "GET") == 0) {
            parse_url(url, info);
            FILE *fp = fopen(info->filename, "wb");
            if (fp) {
                fseek(fp, 0, SEEK_END);
                size_t content_length = ftell(fp);
                fseek(fp, 0, SEEK_SET);
                char *content = (char*)malloc(content_length + 1); // allocate 1 byte for the null terminator
                if (content) {
                    fread(content, 1, content_length, fp);
                    content[content_length] = '\0'; // add null terminator
                    printf("Content-Length: %zd\n", content_length);
                    MHD_add_response_header(connection, "Content-Type", "video/mp4");
                    MHD_add_response_header(connection, "Content-Length", (const char*)content_length);
                    MHD_add_response_data(connection, content, content_length, NULL);
                    free(content);
                    fclose(fp);
                }
            }
            free(info->filename);
        }
    }

    int main() {
        struct MHD_Daemon *d;
        char *error;
        int port;
        struct MHD_Config *config;

        // 创建配置
        config = MHD_create_config_from_stdin(MHD_USE_LOCAL_FILE, NULL, NULL, NULL, NULL);
        config->log_callback = MHD_log_info;

        // 创建服务器
        d = MHD_create_daemon(MHD_USE_THREADING, port, proxy_host, proxy_port, MHD_DEFAULT_HTTPD, NULL, config, NULL);

        // 启动服务器
        if (d) {
            MHD_start_daemon(d);
            printf("Server started on port %d\n", port);
        } else {
            printf("Error: unable to start the daemon\n");
        }

        // 关闭配置和服务器
        MHD_config_free(config);
        MHD_stop_daemon(d);

        return 0;
    }

  • 相关阅读:
    【Java Web】使用ajax在论坛中发布帖子
    【TypeScript】深入学习TypeScript模块
    901 股票价格跨度——Leetcode天天刷(20022.10.21)【单调栈】
    云计算 - 负载均衡SLB方案全解与实战
    label问题排查:打不开标注好的图像
    Python深度学习入门 - - Transformers网络学习笔记
    AI艺术的背后:详解文本生成图像模型【基于 Diffusion Model】
    JAVA并发编程之原子性、可见性与有序性
    竞赛选题 深度学习的视频多目标跟踪实现
    【MongoDB】配置Secondary(从节点) 的 Sync Target(复制源)
  • 原文地址:https://blog.csdn.net/weixin_73725158/article/details/134070171