libctru  v2.4.1
httpc.h
Go to the documentation of this file.
1 /**
2  * @file httpc.h
3  * @brief HTTP service.
4  */
5 #pragma once
6 
7 /// HTTP context.
8 typedef struct {
9  Handle servhandle; ///< Service handle.
10  u32 httphandle; ///< HTTP handle.
11 } httpcContext;
12 
13 /// HTTP request method.
14 typedef enum {
15  HTTPC_METHOD_GET = 0x1,
16  HTTPC_METHOD_POST = 0x2,
17  HTTPC_METHOD_HEAD = 0x3,
18  HTTPC_METHOD_PUT = 0x4,
19  HTTPC_METHOD_DELETE = 0x5
21 
22 /// HTTP request status.
23 typedef enum {
24  HTTPC_STATUS_REQUEST_IN_PROGRESS = 0x5, ///< Request in progress.
25  HTTPC_STATUS_DOWNLOAD_READY = 0x7 ///< Download ready.
27 
28 /// HTTP KeepAlive option.
29 typedef enum {
30  HTTPC_KEEPALIVE_DISABLED = 0x0,
31  HTTPC_KEEPALIVE_ENABLED = 0x1
33 
34 /// Result code returned when a download is pending.
35 #define HTTPC_RESULTCODE_DOWNLOADPENDING 0xd840a02b
36 
37 // Result code returned when asked about a non-existing header.
38 #define HTTPC_RESULTCODE_NOTFOUND 0xd840a028
39 
40 // Result code returned when any timeout function times out.
41 #define HTTPC_RESULTCODE_TIMEDOUT 0xd820a069
42 
43 /// Initializes HTTPC. For HTTP GET the sharedmem_size can be zero. The sharedmem contains data which will be later uploaded for HTTP POST. sharedmem_size should be aligned to 0x1000-bytes.
44 Result httpcInit(u32 sharedmem_size);
45 
46 /// Exits HTTPC.
47 void httpcExit(void);
48 
49 /**
50  * @brief Opens a HTTP context.
51  * @param context Context to open.
52  * @param url URL to connect to.
53  * @param use_defaultproxy Whether the default proxy should be used (0 for default)
54  */
55 Result httpcOpenContext(httpcContext *context, HTTPC_RequestMethod method, const char* url, u32 use_defaultproxy);
56 
57 /**
58  * @brief Closes a HTTP context.
59  * @param context Context to close.
60  */
62 
63 /**
64  * @brief Cancels a HTTP connection.
65  * @param context Context to close.
66  */
68 
69 /**
70  * @brief Adds a request header field to a HTTP context.
71  * @param context Context to use.
72  * @param name Name of the field.
73  * @param value Value of the field.
74  */
75 Result httpcAddRequestHeaderField(httpcContext *context, const char* name, const char* value);
76 
77 /**
78  * @brief Adds a POST form field to a HTTP context.
79  * @param context Context to use.
80  * @param name Name of the field.
81  * @param value Value of the field.
82  */
83 Result httpcAddPostDataAscii(httpcContext *context, const char* name, const char* value);
84 
85 /**
86  * @brief Adds a POST form field with binary data to a HTTP context.
87  * @param context Context to use.
88  * @param name Name of the field.
89  * @param value The binary data to pass as a value.
90  * @param len Length of the binary data which has been passed.
91  */
92 Result httpcAddPostDataBinary(httpcContext *context, const char* name, const u8* value, u32 len);
93 
94 /**
95  * @brief Adds a POST body to a HTTP context.
96  * @param context Context to use.
97  * @param data The data to be passed as raw into the body of the post request.
98  * @param len Length of data passed by data param.
99  */
100 Result httpcAddPostDataRaw(httpcContext *context, const u32* data, u32 len);
101 
102 /**
103  * @brief Begins a HTTP request.
104  * @param context Context to use.
105  */
107 
108 /**
109  * @brief Receives data from a HTTP context.
110  * @param context Context to use.
111  * @param buffer Buffer to receive data to.
112  * @param size Size of the buffer.
113  */
114 Result httpcReceiveData(httpcContext *context, u8* buffer, u32 size);
115 
116 /**
117  * @brief Receives data from a HTTP context with a timeout value.
118  * @param context Context to use.
119  * @param buffer Buffer to receive data to.
120  * @param size Size of the buffer.
121  * @param timeout Maximum time in nanoseconds to wait for a reply.
122  */
123 Result httpcReceiveDataTimeout(httpcContext *context, u8* buffer, u32 size, u64 timeout);
124 
125 /**
126  * @brief Gets the request state of a HTTP context.
127  * @param context Context to use.
128  * @param out Pointer to output the HTTP request state to.
129  */
131 
132 /**
133  * @brief Gets the download size state of a HTTP context.
134  * @param context Context to use.
135  * @param downloadedsize Pointer to output the downloaded size to.
136  * @param contentsize Pointer to output the total content size to.
137  */
138 Result httpcGetDownloadSizeState(httpcContext *context, u32* downloadedsize, u32* contentsize);
139 
140 /**
141  * @brief Gets the response code of the HTTP context.
142  * @param context Context to get the response code of.
143  * @param out Pointer to write the response code to.
144  */
146 
147 /**
148  * @brief Gets the response code of the HTTP context with a timeout value.
149  * @param context Context to get the response code of.
150  * @param out Pointer to write the response code to.
151  * @param timeout Maximum time in nanoseconds to wait for a reply.
152  */
154 
155 /**
156  * @brief Gets a response header field from a HTTP context.
157  * @param context Context to use.
158  * @param name Name of the field.
159  * @param value Pointer to output the value of the field to.
160  * @param valuebuf_maxsize Maximum size of the value buffer.
161  */
162 Result httpcGetResponseHeader(httpcContext *context, const char* name, char* value, u32 valuebuf_maxsize);
163 
164 /**
165  * @brief Adds a trusted RootCA cert to a HTTP context.
166  * @param context Context to use.
167  * @param cert Pointer to DER cert.
168  * @param certsize Size of the DER cert.
169  */
170 Result httpcAddTrustedRootCA(httpcContext *context, const u8 *cert, u32 certsize);
171 
172 /**
173  * @brief Adds a default RootCA cert to a HTTP context.
174  * @param context Context to use.
175  * @param certID ID of the cert to add, see sslc.h.
176  */
177 Result httpcAddDefaultCert(httpcContext *context, SSLC_DefaultRootCert certID);
178 
179 /**
180  * @brief Sets the RootCertChain for a HTTP context.
181  * @param context Context to use.
182  * @param RootCertChain_contexthandle Contexthandle for the RootCertChain.
183  */
184 Result httpcSelectRootCertChain(httpcContext *context, u32 RootCertChain_contexthandle);
185 
186 /**
187  * @brief Sets the ClientCert for a HTTP context.
188  * @param context Context to use.
189  * @param cert Pointer to DER cert.
190  * @param certsize Size of the DER cert.
191  * @param privk Pointer to the DER private key.
192  * @param privk_size Size of the privk.
193  */
194 Result httpcSetClientCert(httpcContext *context, const u8 *cert, u32 certsize, const u8 *privk, u32 privk_size);
195 
196 /**
197  * @brief Sets the default clientcert for a HTTP context.
198  * @param context Context to use.
199  * @param certID ID of the cert to add, see sslc.h.
200  */
201 Result httpcSetClientCertDefault(httpcContext *context, SSLC_DefaultClientCert certID);
202 
203 /**
204  * @brief Sets the ClientCert contexthandle for a HTTP context.
205  * @param context Context to use.
206  * @param ClientCert_contexthandle Contexthandle for the ClientCert.
207  */
208 Result httpcSetClientCertContext(httpcContext *context, u32 ClientCert_contexthandle);
209 
210 /**
211  * @brief Sets SSL options for the context.
212  * The HTTPC SSL option bits are the same as those defined in sslc.h
213  * @param context Context to set flags on.
214  * @param options SSL option flags.
215  */
217 
218 /**
219  * @brief Sets the SSL options which will be cleared for the context.
220  * The HTTPC SSL option bits are the same as those defined in sslc.h
221  * @param context Context to clear flags on.
222  * @param options SSL option flags.
223  */
225 
226 /**
227  * @brief Creates a RootCertChain. Up to 2 RootCertChains can be created under this user-process.
228  * @param RootCertChain_contexthandle Output RootCertChain contexthandle.
229  */
230 Result httpcCreateRootCertChain(u32 *RootCertChain_contexthandle);
231 
232 /**
233  * @brief Destroy a RootCertChain.
234  * @param RootCertChain_contexthandle RootCertChain to use.
235  */
236 Result httpcDestroyRootCertChain(u32 RootCertChain_contexthandle);
237 
238 /**
239  * @brief Adds a RootCA cert to a RootCertChain.
240  * @param RootCertChain_contexthandle RootCertChain to use.
241  * @param cert Pointer to DER cert.
242  * @param certsize Size of the DER cert.
243  * @param cert_contexthandle Optional output ptr for the cert contexthandle(this can be NULL).
244  */
245 Result httpcRootCertChainAddCert(u32 RootCertChain_contexthandle, const u8 *cert, u32 certsize, u32 *cert_contexthandle);
246 
247 /**
248  * @brief Adds a default RootCA cert to a RootCertChain.
249  * @param RootCertChain_contexthandle RootCertChain to use.
250  * @param certID ID of the cert to add, see sslc.h.
251  * @param cert_contexthandle Optional output ptr for the cert contexthandle(this can be NULL).
252  */
253 Result httpcRootCertChainAddDefaultCert(u32 RootCertChain_contexthandle, SSLC_DefaultRootCert certID, u32 *cert_contexthandle);
254 
255 /**
256  * @brief Removes a cert from a RootCertChain.
257  * @param RootCertChain_contexthandle RootCertChain to use.
258  * @param cert_contexthandle Contexthandle of the cert to remove.
259  */
260 Result httpcRootCertChainRemoveCert(u32 RootCertChain_contexthandle, u32 cert_contexthandle);
261 
262 /**
263  * @brief Opens a ClientCert-context. Up to 2 ClientCert-contexts can be open under this user-process.
264  * @param cert Pointer to DER cert.
265  * @param certsize Size of the DER cert.
266  * @param privk Pointer to the DER private key.
267  * @param privk_size Size of the privk.
268  * @param ClientCert_contexthandle Output ClientCert context handle.
269  */
270 Result httpcOpenClientCertContext(const u8 *cert, u32 certsize, const u8 *privk, u32 privk_size, u32 *ClientCert_contexthandle);
271 
272 /**
273  * @brief Opens a ClientCert-context with a default clientclient. Up to 2 ClientCert-contexts can be open under this user-process.
274  * @param certID ID of the cert to add, see sslc.h.
275  * @param ClientCert_contexthandle Output ClientCert context handle.
276  */
277 Result httpcOpenDefaultClientCertContext(SSLC_DefaultClientCert certID, u32 *ClientCert_contexthandle);
278 
279 /**
280  * @brief Closes a ClientCert context.
281  * @param ClientCert_contexthandle ClientCert context to use.
282  */
283 Result httpcCloseClientCertContext(u32 ClientCert_contexthandle);
284 
285 /**
286  * @brief Downloads data from the HTTP context into a buffer.
287  * The *entire* content must be downloaded before using httpcCloseContext(), otherwise httpcCloseContext() will hang.
288  * @param context Context to download data from.
289  * @param buffer Buffer to write data to.
290  * @param size Size of the buffer.
291  * @param downloadedsize Pointer to write the size of the downloaded data to.
292  */
293 Result httpcDownloadData(httpcContext *context, u8* buffer, u32 size, u32 *downloadedsize);
294 
295 /**
296  * @brief Sets Keep-Alive for the context.
297  * @param context Context to set the KeepAlive flag on.
298  * @param option HTTPC_KeepAlive option.
299  */
301 
Result httpcGetResponseStatusCode(httpcContext *context, u32 *out)
Gets the response code of the HTTP context.
Result httpcBeginRequest(httpcContext *context)
Begins a HTTP request.
Result httpcOpenClientCertContext(const u8 *cert, u32 certsize, const u8 *privk, u32 privk_size, u32 *ClientCert_contexthandle)
Opens a ClientCert-context.
Result httpcGetResponseHeader(httpcContext *context, const char *name, char *value, u32 valuebuf_maxsize)
Gets a response header field from a HTTP context.
Result httpcOpenDefaultClientCertContext(SSLC_DefaultClientCert certID, u32 *ClientCert_contexthandle)
Opens a ClientCert-context with a default clientclient.
Result httpcAddPostDataBinary(httpcContext *context, const char *name, const u8 *value, u32 len)
Adds a POST form field with binary data to a HTTP context.
Result httpcGetDownloadSizeState(httpcContext *context, u32 *downloadedsize, u32 *contentsize)
Gets the download size state of a HTTP context.
Result httpcSetKeepAlive(httpcContext *context, HTTPC_KeepAlive option)
Sets Keep-Alive for the context.
Result httpcAddTrustedRootCA(httpcContext *context, const u8 *cert, u32 certsize)
Adds a trusted RootCA cert to a HTTP context.
Result httpcAddPostDataAscii(httpcContext *context, const char *name, const char *value)
Adds a POST form field to a HTTP context.
Result httpcAddDefaultCert(httpcContext *context, SSLC_DefaultRootCert certID)
Adds a default RootCA cert to a HTTP context.
Result httpcDestroyRootCertChain(u32 RootCertChain_contexthandle)
Destroy a RootCertChain.
Result httpcCloseClientCertContext(u32 ClientCert_contexthandle)
Closes a ClientCert context.
Result httpcSelectRootCertChain(httpcContext *context, u32 RootCertChain_contexthandle)
Sets the RootCertChain for a HTTP context.
Result httpcReceiveDataTimeout(httpcContext *context, u8 *buffer, u32 size, u64 timeout)
Receives data from a HTTP context with a timeout value.
Result httpcAddRequestHeaderField(httpcContext *context, const char *name, const char *value)
Adds a request header field to a HTTP context.
Result httpcInit(u32 sharedmem_size)
Initializes HTTPC. For HTTP GET the sharedmem_size can be zero. The sharedmem contains data which wil...
Result httpcDownloadData(httpcContext *context, u8 *buffer, u32 size, u32 *downloadedsize)
Downloads data from the HTTP context into a buffer.
Result httpcSetClientCertDefault(httpcContext *context, SSLC_DefaultClientCert certID)
Sets the default clientcert for a HTTP context.
Result httpcReceiveData(httpcContext *context, u8 *buffer, u32 size)
Receives data from a HTTP context.
Result httpcOpenContext(httpcContext *context, HTTPC_RequestMethod method, const char *url, u32 use_defaultproxy)
Opens a HTTP context.
Result httpcGetRequestState(httpcContext *context, HTTPC_RequestStatus *out)
Gets the request state of a HTTP context.
HTTPC_KeepAlive
HTTP KeepAlive option.
Definition: httpc.h:29
void httpcExit(void)
Exits HTTPC.
Result httpcAddPostDataRaw(httpcContext *context, const u32 *data, u32 len)
Adds a POST body to a HTTP context.
HTTPC_RequestMethod
HTTP request method.
Definition: httpc.h:14
Result httpcCancelConnection(httpcContext *context)
Cancels a HTTP connection.
Result httpcRootCertChainAddDefaultCert(u32 RootCertChain_contexthandle, SSLC_DefaultRootCert certID, u32 *cert_contexthandle)
Adds a default RootCA cert to a RootCertChain.
Result httpcSetClientCert(httpcContext *context, const u8 *cert, u32 certsize, const u8 *privk, u32 privk_size)
Sets the ClientCert for a HTTP context.
Result httpcSetSSLOpt(httpcContext *context, u32 options)
Sets SSL options for the context.
Result httpcSetClientCertContext(httpcContext *context, u32 ClientCert_contexthandle)
Sets the ClientCert contexthandle for a HTTP context.
Result httpcSetSSLClearOpt(httpcContext *context, u32 options)
Sets the SSL options which will be cleared for the context.
Result httpcGetResponseStatusCodeTimeout(httpcContext *context, u32 *out, u64 timeout)
Gets the response code of the HTTP context with a timeout value.
HTTPC_RequestStatus
HTTP request status.
Definition: httpc.h:23
@ HTTPC_STATUS_DOWNLOAD_READY
Download ready.
Definition: httpc.h:25
@ HTTPC_STATUS_REQUEST_IN_PROGRESS
Request in progress.
Definition: httpc.h:24
Result httpcCloseContext(httpcContext *context)
Closes a HTTP context.
Result httpcCreateRootCertChain(u32 *RootCertChain_contexthandle)
Creates a RootCertChain.
Result httpcRootCertChainRemoveCert(u32 RootCertChain_contexthandle, u32 cert_contexthandle)
Removes a cert from a RootCertChain.
Result httpcRootCertChainAddCert(u32 RootCertChain_contexthandle, const u8 *cert, u32 certsize, u32 *cert_contexthandle)
Adds a RootCA cert to a RootCertChain.
HTTP context.
Definition: httpc.h:8
Handle servhandle
Service handle.
Definition: httpc.h:9
u32 httphandle
HTTP handle.
Definition: httpc.h:10
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
u32 Handle
Resource handle.
Definition: types.h:41
s32 Result
Function result.
Definition: types.h:42
uint32_t u32
32-bit unsigned integer
Definition: types.h:23