1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     nhttp.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_NET_COMPATIBLE_NHTTP_NHTTP_H_
17 #define NN_NET_COMPATIBLE_NHTTP_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     NHTTP_ER_DESTROY_NOTSTARTED_CONN  = NHTTP_ER_CODE(81),    /* Please see man pages for details */
93 
94     NHTTP_ER_MAX                    /* Please see man pages for details */
95 } NHTTPError;
96 
97 
98 
99 
100 /* Please see man pages for details
101 
102  */
103 typedef enum nhttp_req_method {
104     NHTTP_REQMETHOD_NONE,   /* Please see man pages for details */
105     NHTTP_REQMETHOD_GET,    /* Please see man pages for details */
106     NHTTP_REQMETHOD_POST,   /* Please see man pages for details */
107     NHTTP_REQMETHOD_HEAD,   /* Please see man pages for details */
108     NHTTP_REQMETHOD_MAX     /* Please see man pages for details */
109 } NHTTPReqMethod;
110 
111 
112 /* Please see man pages for details
113 
114  */
115 typedef enum nhttp_encoding_type {
116     NHTTP_ENCODING_TYPE_AUTO,       /* Please see man pages for details */
117     NHTTP_ENCODING_TYPE_URL,        /* Please see man pages for details */
118     NHTTP_ENCODING_TYPE_MULTIPART,  /* Please see man pages for details */
119     NHTTP_ENCODING_TYPE_MAX         /* Please see man pages for details */
120 } NHTTPEncodingType;
121 
122 /* Please see man pages for details
123 
124  */
125 typedef enum nhttp_postdata_type {
126     NHTTP_POST_DATA_TYPE_URLENCODE,      /* Please see man pages for details */
127     NHTTP_POST_DATA_TYPE_MULTIPART,      /* Please see man pages for details */
128     NHTTP_POST_DATA_TYPE_RAW,            /* Please see man pages for details */
129     NHTTP_POST_DATA_TYPE_MAX             /* Please see man pages for details */
130 } NHTTPPostDataType;
131 
132 
133 
134 /* Please see man pages for details
135 
136  */
137 typedef enum nhttp_connection_event {
138     NHTTP_CONN_EV_NONE,             /* Please see man pages for details */
139     NHTTP_CONN_EV_POST_SEND,        /* Please see man pages for details */
140     NHTTP_CONN_EV_POST_SEND_ALL,    /* Please see man pages for details */
141     NHTTP_CONN_EV_HEADER_RECV_DONE, /* Please see man pages for details */
142     NHTTP_CONN_EV_BODY_RECV_FULL,   /* Please see man pages for details */
143     NHTTP_CONN_EV_BODY_RECV_DONE,   /* Please see man pages for details */
144     NHTTP_CONN_EV_COMPLETE,         /* Please see man pages for details */
145     NHTTP_CONN_EV_MAX               /* Please see man pages for details */
146 } NHTTPConnectionEvent;
147 
148 
149 /* Please see man pages for details
150 
151 
152  */
153 typedef enum nhttp_conn_status {
154     NHTTP_CONNST_CREATED = 0,       /* Please see man pages for details */
155     NHTTP_CONNST_INITIALIZED,       /* Please see man pages for details */
156     NHTTP_CONNST_ENQUEUED_LSN,      /* Please see man pages for details */
157     NHTTP_CONNST_IN_LSN,            /* Please see man pages for details */
158     NHTTP_CONNST_ENQUEUED_COMM,     /* Please see man pages for details */
159     NHTTP_CONNST_CONNECTING,        /* Please see man pages for details */
160     NHTTP_CONNST_SENDING,           /* Please see man pages for details */
161     NHTTP_CONNST_RECEIVING_HEADER,  /* Please see man pages for details */
162     NHTTP_CONNST_RECEIVING_BODY,    /* Please see man pages for details */
163     NHTTP_CONNST_RECEIVED,          /* Please see man pages for details */
164     NHTTP_CONNST_FINISHED           /* Please see man pages for details */
165 } NHTTPConnStatus;
166 
167 
168 /*=======================================================================*
169     Macro definitions
170  *=======================================================================*/
171 
172 
173 /*=======================================================================*
174     Type Definitions
175  *=======================================================================*/
176 
177 /* Memory allocator */
178 typedef void *(*NHTTPAlloc) (u32 name, u32 size, u32 align); /* Please see man pages for details */
179 typedef void (*NHTTPFree) (u32 name, void *ptr); /* Please see man pages for details */
180 
181 /* Other */
182 typedef s32 NHTTPConnectionHandle;      /* Please see man pages for details */
183 
184 /* Please see man pages for details
185 
186  */
187 typedef s32 (*NHTTPConnectionCallback)(NHTTPConnectionHandle handle, NHTTPConnectionEvent event, void *arg);
188 
189 
190 /*=======================================================================*
191     Structure definitions
192  *=======================================================================*/
193 
194 /* Please see man pages for details
195 
196  */
197 typedef struct nhttp_config {
198     NHTTPAlloc alloc;       /* Please see man pages for details */
199     NHTTPFree free;         /* Please see man pages for details */
200     u32 threadPrio;         /* Please see man pages for details */
201     u32 threadNum;          /* Please see man pages for details */
202     u32 queueDepth;         /* Please see man pages for details */
203     u32 singleHostMax;      /* Please see man pages for details */
204 } NHTTPConfig;
205 
206 
207 /* Please see man pages for details
208 
209  */
210 typedef struct nhttp_post_send_arg {
211     const char *label;  /* Please see man pages for details */
212     char *buf;          /* Please see man pages for details */
213     u32 size;           /* Please see man pages for details */
214     u32 offset;         /* Please see man pages for details */
215 } NHTTPPostSendArg;
216 
217 /* Please see man pages for details
218 
219  */
220 typedef struct nhttp_post_send_all_data_arg {
221     char *label;        /* Please see man pages for details */
222     char *buf;          /* Please see man pages for details */
223     u32 size;           /* Please see man pages for details */
224     u32 offset;         /* Please see man pages for details */
225     BOOL isBinary;      /* Please see man pages for details */
226 } NHTTPPostSendAllDataArg;
227 
228 
229 /* Please see man pages for details
230 
231  */
232 typedef struct nhttp_body_buf_arg {
233     char *buf;          /* Please see man pages for details */
234     u32 size;           /* Please see man pages for details */
235     u32 offset;         /* Please see man pages for details */
236 } NHTTPBodyBufArg;
237 
238 
239 /*=======================================================================*
240     Function Prototype Declarations
241  *=======================================================================*/
242 
243 /* Initialize/finalize */
244 s32 NHTTP_Startup(const NHTTPConfig *config);
245 s32 NHTTP_Cleanup(void);
246 
247 /* Get Information */
248 void *NHTTP_CallAlloc(u32 name, u32 size, u32 align);
249 void NHTTP_CallFree(u32 name, void *ptr);
250 
251 /* DNS cache */
252 s32 NHTTP_ClearDnsCaches(void);
253 
254 /* Connection handle */
255 NHTTPConnectionHandle NHTTP_CreateConnection(const char *url, NHTTPReqMethod method, char *bodyBuf, u32 bufLen, NHTTPConnectionCallback callback, void *userParam);
256 s32 NHTTP_DestroyConnection(NHTTPConnectionHandle handle);
257 s32 NHTTP_StartConnection(NHTTPConnectionHandle handle);
258 s32 NHTTP_StartConnectionAsync(NHTTPConnectionHandle handle);
259 s32 NHTTP_CancelConnection(NHTTPConnectionHandle handle);
260 s32 NHTTP_WaitForConnection(NHTTPConnectionHandle handle);
261 s32 NHTTP_GetConnectionStatus(NHTTPConnectionHandle handle);
262 s32 NHTTP_GetConnectionProgress(NHTTPConnectionHandle handle, u32 *received, u32 *contentLen);
263 s32 NHTTP_GetConnectionError(NHTTPConnectionHandle handle);
264 s32 NHTTP_GetConnectionSSLError(NHTTPConnectionHandle handle);
265 void *NHTTP_GetUserParam(NHTTPConnectionHandle handle);
266 
267 /* Set SSL certificate */
268 s32 NHTTP_SetVerifyOption(NHTTPConnectionHandle handle, u32 verifyOption);
269 s32 NHTTP_SetRootCA(NHTTPConnectionHandle handle, u8 *rootCAData, int rootCASize);
270 s32 NHTTP_SetInternalRootCA(NHTTPConnectionHandle handle, u32 internalCaCertId);
271 s32 NHTTP_SetRootCAStore(NHTTPConnectionHandle handle, NSSLCertStoreId rootCAId);
272 s32 NHTTP_SetClientCert(NHTTPConnectionHandle handle, u8 *clientCertData, int clientCertSize, u8 *privateKeyData, int privateKeySize);
273 s32 NHTTP_SetInternalClientCert(NHTTPConnectionHandle handle, u32 internalClientCertId);
274 s32 NHTTP_SetClientCertId(NHTTPConnectionHandle handle, NSSLCertId clientCertId);
275 s32 NHTTP_RemoveClientCert(NHTTPConnectionHandle handle);
276 s32 NHTTP_RemoveRootCA(NHTTPConnectionHandle handle);
277 s32 NHTTP_SetRootCADefault(NHTTPConnectionHandle handle);
278 s32 NHTTP_SetClientCertDefault(NHTTPConnectionHandle handle);
279 s32 NHTTP_DisableVerifyOptionForDebug(NHTTPConnectionHandle handle, u32 verifyOption);
280 
281 /* Connection settings */
282 s32 NHTTP_SetProxy(NHTTPConnectionHandle handle, const char *proxy, u16 port, const char *userName, const char *password);
283 s32 NHTTP_SetProxyDefault(NHTTPConnectionHandle handle);
284 s32 NHTTP_SetBasicAuthorization(NHTTPConnectionHandle handle, const char *userName, const char *password);
285 s32 NHTTP_SetSocketBufferSize(NHTTPConnectionHandle handle, u32 size);
286 s32 NHTTP_SetSocketSendBufferSize(NHTTPConnectionHandle handle, u32 size);
287 s32 NHTTP_SetKeepAliveAvailability(NHTTPConnectionHandle handle, BOOL isUseKeepAlive);
288 
289 /* HTTP request settings - nhttp_req.c */
290 s32 NHTTP_AddHeaderField(NHTTPConnectionHandle handle, const char *label, const char *value);
291 #ifdef NHTTP_POST_BY_CLIENT_HEAP_MODE
292 s32 NHTTP_RemoveExistingAddedPostData(NHTTPConnectionHandle handle, const char *label, void** memory);
293 s32 NHTTP_RemovePostDataHead(NHTTPConnectionHandle handle, void** memory);
294 s32 NHTTP_GetPostDataRaw(NHTTPConnectionHandle handle, void** memory);
295 size_t NHTTP_CalculateElementSize(const char* label, u32 valueLen);
296 #endif
297 
298 #ifdef NHTTP_POST_BY_CLIENT_HEAP_MODE
299 s32 NHTTP_AddPostDataAscii(NHTTPConnectionHandle handle, const char *label, const char *value, void* memory);
300 s32 NHTTP_AddPostDataBinary(NHTTPConnectionHandle handle, const char *label, const void *value, u32 length, void* memory);
301 s32 NHTTP_AddPostDataRaw(NHTTPConnectionHandle handle, const void *value, u32 length, void* memory);
302 #else
303 s32 NHTTP_AddPostDataAscii(NHTTPConnectionHandle handle, const char *label, const char *value);
304 s32 NHTTP_AddPostDataBinary(NHTTPConnectionHandle handle, const char *label, const void *value, u32 length);
305 s32 NHTTP_AddPostDataRaw(NHTTPConnectionHandle handle, const void *value, u32 length);
306 #endif
307 s32 NHTTP_SetPostDataEncoding(NHTTPConnectionHandle handle, NHTTPEncodingType type);
308 s32 NHTTP_SetPostDataLazySet(NHTTPConnectionHandle handle, NHTTPPostDataType dataType, u32 length);
309 
310 /* Get HTTP response - nhttp_res.c */
311 s32 NHTTP_GetBodyBuffer(NHTTPConnectionHandle handle, char **bodyBuffer, u32 *size);
312 s32 NHTTP_AllocBodyBuffer(NHTTPConnectionHandle handle, u32 size);
313 s32 NHTTP_FreeBodyBuffer(NHTTPConnectionHandle handle);
314 s32 NHTTP_GetHeaderField(NHTTPConnectionHandle, const char *label, char *value, u32 maxLen);
315 s32 NHTTP_GetHeaderAll(NHTTPConnectionHandle handle, char *value, u32 maxLen);
316 s32 NHTTP_GetResultCode(NHTTPConnectionHandle handle);
317 
318 
319 #ifdef __cplusplus
320 } /* extern "C" */
321 #endif
322 
323 #endif /* NN_NET_COMPATIBLE_NHTTP_NHTTP_H_ */
324