libcurl http sever C代码
时间: 2024-06-11 13:06:29 浏览: 24
以下是一个简单的使用libcurl实现HTTP服务器的C代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define PORT 8080
static size_t handle_request(void *ptr, size_t size, size_t nmemb, void *userdata) {
CURL *curl = (CURL *)userdata;
char *request = (char *)malloc(size * nmemb + 1);
memcpy(request, ptr, size * nmemb);
request[size * nmemb] = '\0';
printf("Received request:\n%s\n", request);
if (strstr(request, "GET /") == request) {
const char *response = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n<html><body><h1>Hello, World!</h1></body></html>";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, response);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(response));
} else {
const char *response = "HTTP/1.1 404 Not Found\nContent-Type: text/html\n\n<html><body><h1>404 Not Found</h1></body></html>";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, response);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(response));
}
free(request);
return size * nmemb;
}
int main() {
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_request);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
CURLcode res;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: text/html");
if (curl) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_PORT, PORT);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_slist_free_all(headers);
}
return 0;
}
```
这个例子实现了一个简单的HTTP服务器,监听8080端口,并在收到GET请求时返回一个Hello World的HTML页面。如果收到其他类型的请求,服务器将返回404 Not Found。
在这个例子中,我们使用了libcurl提供的CURLOPT_WRITEFUNCTION选项来设置一个回调函数来处理接收到的请求。我们使用了curl_easy_setopt函数来设置libcurl的参数,并使用curl_easy_perform函数来启动HTTP服务器。我们还使用了curl_slist_append函数来添加一个Content-Type头部。
该程序可以使用以下命令编译:
```
gcc -o http_server http_server.c -lcurl
```
然后可以使用以下命令来运行HTTP服务器:
```
./http_server
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)