#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <microhttpd.h>
#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;
}