C++ 言語のコード例

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

#define MAX_URL_LEN 256

typedef struct {     char *url;     char *ファイル名; URL_情報;


void parse_url(const char *url, url_info *info) {     info->url = url;     info->ファイル名 = (char*)malloc(strlen(url) + 5); // ".mp4" に 5 バイトを割り当てます}


void handle_request(struct MHD_Connection *接続, const char *url, const char *メソッド, const char *バージョン, 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->ファイル名, "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); // null ターミネータに 1 バイトを割り当て             if (content) {                 fread(content, 1, content_length, fp);                 コンテンツ[コンテンツの長さ] = '\0'; // null ターミネータを追加します                 printf("Content-Length: %zd\n", content_length);                 MHD_add_response_header(接続, "Content-Type", "video/mp4");                 MHD_add_response_header(connection, "Content-Length", (const char*)content_length);                 MHD_add_response_data(接続, コンテンツ, content_length, NULL);                 無料(コンテンツ);                 fclose(fp);             free (情報         -         >ファイル名);     } }






















int main() {     struct MHD_Daemon *d;     文字 * エラー;     整数ポート;     struct MHD_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("サーバーはポート %d で起動しました\n", port);     } else {         printf("エラー: デーモンを開始できません\n");     }




    // 設定とサーバーを閉じます
    MHD_config_free(config);
    MHD_stop_daemon(d);

    0を返します。
}

Supongo que te gusta

Origin blog.csdn.net/weixin_73725158/article/details/134070171
Recomendado
Clasificación