-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.h
More file actions
76 lines (68 loc) 路 2.04 KB
/
Copy pathhttp.h
File metadata and controls
76 lines (68 loc) 路 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file http.h
* @brief Simple HTTP/HTTPS client API.
*
* Provides minimal API for downloading files and fetching URLs.
* Implementation uses libcurl with platform-native TLS backends.
*
* Usage:
* // Download with progress bar
* http_download(url, path, http_default_progress, NULL);
*
* // Download silently
* http_download(url, path, NULL, NULL);
*
* // Fetch into memory
* struct sbuf body = SBUF_INIT;
* int status = http_get("https://api.example.com/v1/info", &body);
* if (status == 200)
* printf("%s\n", body.buf);
* sbuf_release(&body);
*/
#ifndef HTTP_H
#define HTTP_H
#include <stddef.h>
struct sbuf;
/**
* @brief Progress callback for http_download.
*
* @param total Total bytes expected (0 if unknown).
* @param now Bytes downloaded so far.
* @param ctx Opaque context passed to http_download.
*/
typedef void (*http_progress_fn)(size_t total, size_t now, void *ctx);
/**
* @brief Default progress bar printed to stderr.
*
* Shows percentage and a simple bar. Pass as the progress argument
* to http_download for standard progress output.
*/
void http_default_progress(size_t total, size_t now, void *ctx);
/**
* @brief Download a URL to a file.
*
* @param url HTTPS or HTTP URL.
* @param path Destination file path.
* @param progress Progress callback, or NULL for silent download.
* @param ctx Opaque context passed to progress callback.
* @return 0 on success, -1 on error.
*/
int http_download(const char *url, const char *path, http_progress_fn progress,
void *ctx);
/**
* @brief Fetch a URL into memory.
*
* Appends the response body to @p body. The caller should
* initialize body with SBUF_INIT before calling.
*
* @param url HTTPS or HTTP URL.
* @param body Destination sbuf for response body.
* @return HTTP status code (e.g. 200) on success, -1 on error.
*/
int http_get(const char *url, struct sbuf *body);
#endif /* HTTP_H */