1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     nhttp.h
4   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law. They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10   $Rev: 36188 $
11  *---------------------------------------------------------------------------
12 
13 
14 */
15 
16 #ifndef NHTTP_H_
17 #define NHTTP_H_
18 
19 #include <nn/net/compatible/nssl.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /*=======================================================================*
26     Constant definitions
27  *=======================================================================*/
28 /* Please see man pages for details
29 
30  */
31 #define NHTTP_CONNECTION_MAX        14          /* Please see man pages for details */
32 
33 /* Please see man pages for details
34 
35  */
36 #define NHTTP_ER_BASE           0xffffe000
37 #define NHTTP_ER_CODE(code)     ((s32)(NHTTP_ER_BASE|(code)))
38 
39 typedef enum nhttp_error {
40     NHTTP_ER_NONE               = 0,                /* Please see man pages for details */
41     NHTTP_ER_INVALID_STATUS     = NHTTP_ER_CODE(1), /* Please see man pages for details */
42     NHTTP_ER_INVALID_PARAM      = NHTTP_ER_CODE(2), /* Please see man pages for details */
43     NHTTP_ER_NOMEM              = NHTTP_ER_CODE(3), /* Please see man pages for details */
44     NHTTP_ER_CREATE_EVENT       = NHTTP_ER_CODE(4), /* Please see man pages for details */
45     NHTTP_ER_CREATE_MUTEX       = NHTTP_ER_CODE(5), /* Please see man pages for details */
46     NHTTP_ER_CREATE_QUEUE       = NHTTP_ER_CODE(6), /* Please see man pages for details */
47     NHTTP_ER_CREATE_THREAD      = NHTTP_ER_CODE(7), /* Please see man pages for details */
48 
49     /* Message queue */
50     NHTTP_ER_MSGQ_SEND_LSN      = NHTTP_ER_CODE(10),    /* Please see man pages for details */
51     NHTTP_ER_MSGQ_RECV_LSN      = NHTTP_ER_CODE(11),    /* Please see man pages for details */
52     NHTTP_ER_MSGQ_RECV_COMM     = NHTTP_ER_CODE(12),    /* Please see man pages for details */
53 
54     /* Connection handle */
55     NHTTP_ER_CONN_NOMORE        = NHTTP_ER_CODE(20),    /* Please see man pages for details */
56     NHTTP_ER_CONN_NOSUCH        = NHTTP_ER_CODE(21),    /* Please see man pages for details */
57     NHTTP_ER_CONN_STATUS        = NHTTP_ER_CODE(22),    /* Please see man pages for details */
58     NHTTP_ER_CONN_ADD           = NHTTP_ER_CODE(23),    /* Please see man pages for details */
59     NHTTP_ER_CONN_CANCELED      = NHTTP_ER_CODE(24),    /* Please see man pages for details */
60     NHTTP_ER_CONN_HOST_MAX      = NHTTP_ER_CODE(25),    /* Please see man pages for details */
61 
62     /* Request */
63     NHTTP_ER_REQ_URL            = NHTTP_ER_CODE(30),    /* Please see man pages for details */
64     NHTTP_ER_REQ_CONNMSG_PORT   = NHTTP_ER_CODE(31),    /* Please see man pages for details */
65     NHTTP_ER_REQ_UNKNOWN_METHOD = NHTTP_ER_CODE(32),    /* Please see man pages for details */
66 
67     /* Response */
68     NHTTP_ER_RES_HEADER         = NHTTP_ER_CODE(40),    /* Please see man pages for details */
69     NHTTP_ER_RES_NONEWLINE      = NHTTP_ER_CODE(41),    /* Please see man pages for details */
70     NHTTP_ER_RES_BODYBUF        = NHTTP_ER_CODE(42),    /* Please see man pages for details */
71 
72     /* POST */
73     NHTTP_ER_POST_ADDED_ANOTHER = NHTTP_ER_CODE(50),    /* Please see man pages for details */
74     NHTTP_ER_POST_BOUNDARY      = NHTTP_ER_CODE(51),    /* Please see man pages for details */
75     NHTTP_ER_POST_SEND          = NHTTP_ER_CODE(52),    /* Please see man pages for details */
76     NHTTP_ER_POST_UNKNOWN_ENCTYPE = NHTTP_ER_CODE(53), /* Please see man pages for details */
77     NHTTP_ER_POST_NODATA        = NHTTP_ER_CODE(54),    /* Please see man pages for details */
78 
79     /* SSL */
80     NHTTP_ER_SSL                = NHTTP_ER_CODE(60),    /* Please see man pages for details */
81     NHTTP_ER_SSL_CERT_EXIST     = NHTTP_ER_CODE(61),    /* Please see man pages for details */
82 
83     /* socket */
84     NHTTP_ER_SOC_DNS            = NHTTP_ER_CODE(70),    /* Please see man pages for details */
85     NHTTP_ER_SOC_SEND           = NHTTP_ER_CODE(71),    /* Please see man pages for details */
86     NHTTP_ER_SOC_RECV           = NHTTP_ER_CODE(72),    /* Please see man pages for details */
87     NHTTP_ER_SOC_CONN           = NHTTP_ER_CODE(73),    /* Please see man pages for details */
88     NHTTP_ER_SOC_KEEPALIVE_DISCONNECTED           = NHTTP_ER_CODE(74),    /* Please see man pages for details */
89 
90     /* Other */
91     NHTTP_ER_GET_DEFAULT_PROXY  = NHTTP_ER_CODE(80),    /* Please see man pages for details */
92 
93     NHTTP_ER_MAX                    /* Please see man pages for details */
94 } NHTTPError;
95 
96 
97 
98 
99 /* Please see man pages for details
100 
101  */
102 typedef enum nhttp_req_method {
103     NHTTP_REQMETHOD_NONE,   /* Please see man pages for details */
104     NHTTP_REQMETHOD_GET,    /* Please see man pages for details */
105     NHTTP_REQMETHOD_POST,   /* Please see man pages for details */
106     NHTTP_REQMETHOD_HEAD,   /* Please see man pages for details */
107     NHTTP_REQMETHOD_MAX     /* Please see man pages for details */
108 } NHTTPReqMethod;
109 
110 
111 /* Please see man pages for details
112 
113  */
114 typedef enum nhttp_encoding_type {
115     NHTTP_ENCODING_TYPE_AUTO,       /* Please see man pages for details */
116     NHTTP_ENCODING_TYPE_URL,        /* Please see man pages for details */
117     NHTTP_ENCODING_TYPE_MULTIPART,  /* Please see man pages for details */
118     NHTTP_ENCODING_TYPE_MAX         /* Please see man pages for details */
119 } NHTTPEncodingType;
120 
121 /* Please see man pages for details
122 
123  */
124 typedef enum nhttp_postdata_type {
125     NHTTP_POST_DATA_TYPE_URLENCODE,      /* Please see man pages for details */
126     NHTTP_POST_DATA_TYPE_MULTIPART,      /* Please see man pages for details */
127     NHTTP_POST_DATA_TYPE_RAW,            /* Please see man pages for details */
128     NHTTP_POST_DATA_TYPE_MAX             /* Please see man pages for details */
129 } NHTTPPostDataType;
130 
131 
132 
133 /* Please see man pages for details
134 
135  */
136 typedef enum nhttp_connection_event {
137     NHTTP_CONN_EV_NONE,             /* Please see man pages for details */
138     NHTTP_CONN_EV_POST_SEND,        /* Please see man pages for details */
139     NHTTP_CONN_EV_POST_SEND_ALL,    /* Please see man pages for details */
140     NHTTP_CONN_EV_HEADER_RECV_DONE, /* Please see man pages for details */
141     NHTTP_CONN_EV_BODY_RECV_FULL,   /* Please see man pages for details */
142     NHTTP_CONN_EV_BODY_RECV_DONE,   /* Please see man pages for details */
143     NHTTP_CONN_EV_COMPLETE,         /* Please see man pages for details */
144     NHTTP_CONN_EV_MAX               /* Please see man pages for details */
145 } NHTTPConnectionEvent;
146 
147 
148 /* Please see man pages for details
149 
150 
151  */
152 typedef enum nhttp_conn_status {
153     NHTTP_CONNST_CREATED = 0,       /* Please see man pages for details */
154     NHTTP_CONNST_INITIALIZED,       /* Please see man pages for details */
155     NHTTP_CONNST_ENQUEUED_LSN,      /* Please see man pages for details */
156     NHTTP_CONNST_IN_LSN,            /* Please see man pages for details */
157     NHTTP_CONNST_ENQUEUED_COMM,     /* Please see man pages for details */
158     NHTTP_CONNST_CONNECTING,        /* Please see man pages for details */
159     NHTTP_CONNST_SENDING,           /* Please see man pages for details */
160     NHTTP_CONNST_RECEIVING_HEADER,  /* Please see man pages for details */
161     NHTTP_CONNST_RECEIVING_BODY,    /* Please see man pages for details */
162     NHTTP_CONNST_RECEIVED,          /* Please see man pages for details */
163     NHTTP_CONNST_FINISHED           /* Please see man pages for details */
164 } NHTTPConnStatus;
165 
166 
167 /*=======================================================================*
168     Macro definitions
169  *=======================================================================*/
170 
171 
172 /*=======================================================================*
173     Type Definitions
174  *=======================================================================*/
175 
176 /* Memory allocator */
177 typedef void *(*NHTTPAlloc) (u32 name, u32 size, u32 align); /* Please see man pages for details */
178 typedef void (*NHTTPFree) (u32 name, void *ptr); /* Please see man pages for details */
179 
180 /* Other */
181 typedef s32 NHTTPConnectionHandle;      /* Please see man pages for details */
182 
183 /* Please see man pages for details
184 
185  */
186 typedef s32 (*NHTTPConnectionCallback)(NHTTPConnectionHandle handle, NHTTPConnectionEvent event, void *arg);
187 
188 
189 /*=======================================================================*
190     Structure definitions
191  *=======================================================================*/
192 
193 /* Please see man pages for details
194 
195  */
196 typedef struct nhttp_config {
197     NHTTPAlloc alloc;       /* Please see man pages for details */
198     NHTTPFree free;         /* Please see man pages for details */
199     u32 threadPrio;         /* Please see man pages for details */
200     u32 threadNum;          /* Please see man pages for details */
201     u32 queueDepth;         /* Please see man pages for details */
202     u32 singleHostMax;      /* Please see man pages for details */
203 } NHTTPConfig;
204 
205 
206 /* Please see man pages for details
207 
208  */
209 typedef struct nhttp_post_send_arg {
210     const char *label;  /* Please see man pages for details */
211     char *buf;          /* Please see man pages for details */
212     u32 size;           /* Please see man pages for details */
213     u32 offset;         /* Please see man pages for details */
214 } NHTTPPostSendArg;
215 
216 /* Please see man pages for details
217 
218  */
219 typedef struct nhttp_post_send_all_data_arg {
220     char *label;        /* Please see man pages for details */
221     char *buf;          /* Please see man pages for details */
222     u32 size;           /* Please see man pages for details */
223     u32 offset;         /* Please see man pages for details */
224     BOOL isBinary;      /* Please see man pages for details */
225 } NHTTPPostSendAllDataArg;
226 
227 
228 /* Please see man pages for details
229 
230  */
231 typedef struct nhttp_body_buf_arg {
232     char *buf;          /* Please see man pages for details */
233     u32 size;           /* Please see man pages for details */
234     u32 offset;         /* Please see man pages for details */
235 } NHTTPBodyBufArg;
236 
237 
238 /*=======================================================================*
239     Function Prototype Declarations
240  *=======================================================================*/
241 
242 /* Initialization/termination */
243 s32 NHTTP_Startup(const NHTTPConfig *config);
244 s32 NHTTP_Cleanup(void);
245 
246 /* Get Information */
247 void *NHTTP_CallAlloc(u32 name, u32 size, u32 align);
248 void NHTTP_CallFree(u32 name, void *ptr);
249 
250 /* Clear DNS caches */
251 s32 NHTTP_ClearDnsCaches(void);
252 
253 /* Connection handle */
254 NHTTPConnectionHandle NHTTP_CreateConnection(const char *url, NHTTPReqMethod method, char *bodyBuf, u32 bufLen, NHTTPConnectionCallback callback, void *userParam);
255 s32 NHTTP_DestroyConnection(NHTTPConnectionHandle handle);
256 s32 NHTTP_StartConnection(NHTTPConnectionHandle handle);
257 s32 NHTTP_StartConnectionAsync(NHTTPConnectionHandle handle);
258 s32 NHTTP_CancelConnection(NHTTPConnectionHandle handle);
259 s32 NHTTP_WaitForConnection(NHTTPConnectionHandle handle);
260 s32 NHTTP_GetConnectionStatus(NHTTPConnectionHandle handle);
261 s32 NHTTP_GetConnectionProgress(NHTTPConnectionHandle handle, u32 *received, u32 *contentLen);
262 s32 NHTTP_GetConnectionError(NHTTPConnectionHandle handle);
263 s32 NHTTP_GetConnectionSSLError(NHTTPConnectionHandle handle);
264 void *NHTTP_GetUserParam(NHTTPConnectionHandle handle);
265 
266 /* SSL certificate */
267 s32 NHTTP_SetVerifyOption(NHTTPConnectionHandle handle, u32 verifyOption);
268 s32 NHTTP_SetRootCA(NHTTPConnectionHandle handle, u8 *rootCAData, int rootCASize);
269 s32 NHTTP_SetInternalRootCA(NHTTPConnectionHandle handle, u32 internalCaCertId);
270 s32 NHTTP_SetRootCaStore(NHTTPConnectionHandle handle, NSSLCertStoreId rootCAId);
271 s32 NHTTP_SetClientCert(NHTTPConnectionHandle handle, u8 *clientCertData, int clientCertSize, u8 *privateKeyData, int privateKeySize);
272 s32 NHTTP_SetInternalClientCert(NHTTPConnectionHandle handle, u32 internalClientCertId);
273 s32 NHTTP_SetClientCertId(NHTTPConnectionHandle handle, NSSLCertId clientCertId);
274 s32 NHTTP_RemoveClientCert(NHTTPConnectionHandle handle);
275 s32 NHTTP_RemoveRootCA(NHTTPConnectionHandle handle);
276 s32 NHTTP_SetRootCADefault(NHTTPConnectionHandle handle);
277 s32 NHTTP_SetClientCertDefault(NHTTPConnectionHandle handle);
278 s32 NHTTP_DisableVerifyOptionForDebug(NHTTPConnectionHandle handle, u32 verifyOption);
279 
280 /* Connection settings */
281 s32 NHTTP_SetProxy(NHTTPConnectionHandle handle, const char *proxy, u16 port, const char *userName, const char *password);
282 s32 NHTTP_SetProxyDefault(NHTTPConnectionHandle handle);
283 s32 NHTTP_SetBasicAuthorization(NHTTPConnectionHandle handle, const char *userName, const char *password);
284 s32 NHTTP_SetSocketBufferSize(NHTTPConnectionHandle handle, u32 size);
285 s32 NHTTP_SetSocketSendBufferSize(NHTTPConnectionHandle handle, u32 size);
286 s32 NHTTP_SetKeepAliveAvailability(NHTTPConnectionHandle handle, BOOL isUseKeepAlive);
287 
288 /* HTTP request settings - nhttp_req.c */
289 s32 NHTTP_AddHeaderField(NHTTPConnectionHandle handle, const char *label, const char *value);
290 #ifdef NHTTP_POST_BY_CLIENT_HEAP_MODE
291 s32 NHTTP_RemoveExistingAddedPostData(NHTTPConnectionHandle handle, const char *label, void** memory);
292 s32 NHTTP_RemovePostDataHead(NHTTPConnectionHandle handle, void** memory);
293 s32 NHTTP_GetPostDataRaw(NHTTPConnectionHandle handle, void** memory);
294 size_t NHTTP_CalculateElementSize(const char* label, u32 valueLen);
295 s32 NHTTP_AddPostDataAscii(NHTTPConnectionHandle handle, const char *label, const char *value, void* memory);
296 s32 NHTTP_AddPostDataBinary(NHTTPConnectionHandle handle, const char *label, const void *value, u32 length, void* memory);
297 s32 NHTTP_AddPostDataRaw(NHTTPConnectionHandle handle, const void *value, u32 length, void* memory);
298 #else
299 s32 NHTTP_AddPostDataAscii(NHTTPConnectionHandle handle, const char *label, const char *value);
300 s32 NHTTP_AddPostDataBinary(NHTTPConnectionHandle handle, const char *label, const void *value, u32 length);
301 s32 NHTTP_AddPostDataRaw(NHTTPConnectionHandle handle, const void *value, u32 length);
302 #endif
303 s32 NHTTP_SetPostDataEncoding(NHTTPConnectionHandle handle, NHTTPEncodingType type);
304 s32 NHTTP_SetPostDataLazySet(NHTTPConnectionHandle handle, NHTTPPostDataType dataType);
305 
306 /* Get HTTP response - nhttp_res.c */
307 s32 NHTTP_GetBodyBuffer(NHTTPConnectionHandle handle, char **bodyBuffer, u32 *size);
308 s32 NHTTP_AllocBodyBuffer(NHTTPConnectionHandle handle, u32 size);
309 s32 NHTTP_FreeBodyBuffer(NHTTPConnectionHandle handle);
310 s32 NHTTP_GetHeaderField(NHTTPConnectionHandle, const char *label, char *value, u32 maxLen);
311 s32 NHTTP_GetHeaderAll(NHTTPConnectionHandle handle, char *value, u32 maxLen);
312 s32 NHTTP_GetResultCode(NHTTPConnectionHandle handle);
313 
314 
315 #ifdef __cplusplus
316 } /* extern "C" */
317 #endif
318 
319 #endif /* NHTTP_H_ */
320