• 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;
    }

  • 相关阅读:
    simulink平面五杆机构运动学仿真
    多机器人三角形编队的实现
    Redis的各种部署
    单片机人机交互--矩阵按键
    !力扣 108. 将有序数组转换为二叉搜索树
    30天自制C++服务器day16-重构服务器、使用智能指针
    k8s.3-kubeadm部署单Master节点kubernetes集群 1.21
    RabbitMQ系列【8】消息可靠性之ACK机制
    FTX的前世今生:崛起、辉煌与崩塌
    某车企笔试题解答(2)
  • 原文地址:https://blog.csdn.net/weixin_73725158/article/details/134070171