Exemple de code de langage C++

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

#définir MAX_URL_LEN 256

typedef struct {     char *url;     char *nom de fichier; } url_info;


void parse_url(const char *url, url_info *info) {     info->url = url;     info->filename = (char*)malloc(strlen(url) + 5); // alloue 5 octets pour ".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);         FICHIER *fp = fopen(info->filename, "wb");         si (fp) {             fseek(fp, 0, SEEK_END);             size_t content_length = ftell(fp);             fseek(fp, 0, SEEK_SET);             char *content = (char*)malloc(content_length + 1); // alloue 1 octet pour le terminateur nul             if (content) {                 fread(content, 1, content_length, fp);                 contenu[content_length] = '\0'; // ajoute un terminateur nul                 printf("Content-Length: %zd\n", content_length);                 MHD_add_response_header(connexion, "Content-Type", "video/mp4");                 MHD_add_response_header(connection, "Content-Length", (const char*)content_length);                 MHD_add_response_data(connexion, contenu, content_length, NULL);                 gratuit (contenu);                 fclose(fp);             }         }         free(info->nom du fichier);     } }






















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



    // Fichier de configuration
    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);

    // Fonctionnement
    if (d) {         MHD_start_daemon(d);         printf("Serveur démarré sur le port %d\n", port);     } else {         printf("Erreur : impossible de démarrer le démon\n");     }




    // Ferme la configuration et le serveur
    MHD_config_free(config);
    MHD_stop_daemon(d);

    renvoie 0 ;
}

Je suppose que tu aimes

Origine blog.csdn.net/weixin_73725158/article/details/134070171
conseillé
Classement