comparison foosdk/sdk/foobar2000/SDK/http_client.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 //! Implemented by file object returned by http_request::run methods. Allows you to retrieve various additional information returned by the server. \n
4 //! Warning: reply status may change when seeking on the file object since seek operations often require a new HTTP request to be fired.
5 class NOVTABLE http_reply : public service_base {
6 FB2K_MAKE_SERVICE_INTERFACE(http_reply, service_base)
7 public:
8 //! Retrieves the status line, eg. "200 OK".
9 virtual void get_status(pfc::string_base & out) = 0;
10 //! Retrieves a HTTP header value, eg. "content-type". Note that get_http_header("content-type", out) is equivalent to get_content_type(out). If there are multiple matching header entries, value of the first one will be returned.
11 virtual bool get_http_header(const char * name, pfc::string_base & out) = 0;
12 //! Retrieves a HTTP header value, eg. "content-type". If there are multiple matching header entries, this will return all their values, delimited by \r\n.
13 virtual bool get_http_header_multi(const char * name, pfc::string_base & out) = 0;
14 };
15
16 class NOVTABLE http_request : public service_base {
17 FB2K_MAKE_SERVICE_INTERFACE(http_request, service_base)
18 public:
19 //! Adds a HTTP request header line.
20 //! @param line Request to be added, without trailing \r\n.
21 virtual void add_header(const char * line) = 0;
22 //! Runs the request on the specified URL. Throws an exception on failure (connection error, invalid response from the server, reply code other than 2XX), returns a file::ptr interface to the stream on success.
23 virtual file::ptr run(const char * url, abort_callback & abort) = 0;
24 //! Runs the request on the specified URL. Throws an exception on failure but returns normally if the HTTP server returned a valid response other than 2XX, so the caller can still parse the received data stream if the server has returned an error.
25 virtual file::ptr run_ex(const char * url, abort_callback & abort) = 0;
26
27 void add_header(const char * name, const char * value) {
28 add_header(PFC_string_formatter() << name << ": " << value);
29 }
30 };
31
32 class NOVTABLE http_request_post : public http_request {
33 FB2K_MAKE_SERVICE_INTERFACE(http_request_post, http_request);
34 public:
35 //! Adds a HTTP POST field.
36 //! @param name Field name.
37 //! @param fileName File name to be included in the POST request; leave empty ("") not to send a file name.
38 //! @param contentType Content type of the entry; leave empty ("") not to send content type.
39 virtual void add_post_data(const char * name, const void * data, t_size dataSize, const char * fileName, const char * contentType) = 0;
40
41 void add_post_data(const char * name, const char * value) { add_post_data(name, value, strlen(value), "", ""); }
42 };
43
44 //! \since 1.5
45 class NOVTABLE http_request_post_v2 : public http_request_post {
46 FB2K_MAKE_SERVICE_INTERFACE(http_request_post_v2, http_request_post);
47 public:
48 virtual void set_post_data(const void* blob, size_t bytes, const char* contentType) = 0;
49 };
50
51 class NOVTABLE http_client : public service_base {
52 FB2K_MAKE_SERVICE_COREAPI(http_client)
53 public:
54 //! Creates a HTTP request object.
55 //! @param type Request type. Currently supported: "GET" and "POST". Throws pfc::exception_not_implemented for unsupported values.
56 virtual http_request::ptr create_request(const char * type) = 0;
57 };