// Result code returned when asked about a non-existing header
#define HTTPC_RESULTCODE_NOTFOUND 0xd840a028
-/// Initializes HTTPC.
-Result httpcInit(void);
+/// 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.
+Result httpcInit(u32 sharedmem_size);
/// Exits HTTPC.
void httpcExit(void);
*/
Result httpcAddRequestHeaderField(httpcContext *context, char* name, char* value);
+/**
+ * @brief Adds a POST form field to a HTTP context.
+ * @param context Context to use.
+ * @param name Name of the field.
+ * @param value Value of the field.
+ */
+Result httpcAddPostDataAscii(httpcContext *context, char* name, char* value);
+
/**
* @brief Begins a HTTP request.
* @param context Context to use.
* @brief Initializes HTTPC.
* @param handle HTTPC service handle to use.
*/
-Result HTTPC_Initialize(Handle handle);
+Result HTTPC_Initialize(Handle handle, u32 sharedmem_size, Handle sharedmem_handle);
/**
* @brief Initializes a HTTP connection session.
*/
Result HTTPC_AddRequestHeaderField(Handle handle, Handle contextHandle, char* name, char* value);
+/**
+ * @brief Adds a POST form field to a HTTP context.
+ * @param handle HTTPC service handle to use.
+ * @param contextHandle HTTP context handle to use.
+ * @param name Name of the field.
+ * @param value of the field.
+ */
+Result HTTPC_AddPostDataAscii(Handle handle, Handle contextHandle, char* name, char* value);
+
/**
* @brief Begins a HTTP request.
* @param handle HTTPC service handle to use.
#include <string.h>
+#include <stdlib.h>
+#include <malloc.h>
#include <3ds/types.h>
#include <3ds/result.h>
#include <3ds/svc.h>
Handle __httpc_servhandle;
static int __httpc_refcount;
-Result httpcInit(void)
+u32 *__httpc_sharedmem_addr;
+static u32 __httpc_sharedmem_size;
+static Handle __httpc_sharedmem_handle;
+
+Result httpcInit(u32 sharedmem_size)
{
Result ret=0;
ret = srvGetServiceHandle(&__httpc_servhandle, "http:C");
if (R_SUCCEEDED(ret))
{
- ret = HTTPC_Initialize(__httpc_servhandle);
+ __httpc_sharedmem_size = sharedmem_size;
+ __httpc_sharedmem_handle = 0;
+
+ if(__httpc_sharedmem_size)
+ {
+ __httpc_sharedmem_addr = memalign(0x1000, __httpc_sharedmem_size);
+ if(__httpc_sharedmem_addr==NULL)ret = -1;
+
+ if (R_SUCCEEDED(ret))ret = svcCreateMemoryBlock(&__httpc_sharedmem_handle, (u32)__httpc_sharedmem_addr, __httpc_sharedmem_size, 0, 3);
+ }
+
+ if (R_SUCCEEDED(ret))ret = HTTPC_Initialize(__httpc_servhandle, __httpc_sharedmem_size, __httpc_sharedmem_handle);
if (R_FAILED(ret)) svcCloseHandle(__httpc_servhandle);
}
if (R_FAILED(ret)) AtomicDecrement(&__httpc_refcount);
+ if (R_FAILED(ret) && __httpc_sharedmem_handle)
+ {
+ svcCloseHandle(__httpc_sharedmem_handle);
+ __httpc_sharedmem_handle = 0;
+ __httpc_sharedmem_size = 0;
+
+ free(__httpc_sharedmem_addr);
+ __httpc_sharedmem_addr = NULL;
+ }
+
return ret;
}
{
if (AtomicDecrement(&__httpc_refcount)) return;
svcCloseHandle(__httpc_servhandle);
+
+ if(__httpc_sharedmem_handle)
+ {
+ svcCloseHandle(__httpc_sharedmem_handle);
+ __httpc_sharedmem_handle = 0;
+ __httpc_sharedmem_size = 0;
+
+ free(__httpc_sharedmem_addr);
+ __httpc_sharedmem_addr = NULL;
+ }
}
Result httpcOpenContext(httpcContext *context, HTTPC_RequestMethod method, char* url, u32 use_defaultproxy)
return HTTPC_AddRequestHeaderField(context->servhandle, context->httphandle, name, value);
}
+Result httpcAddPostDataAscii(httpcContext *context, char* name, char* value)
+{
+ return HTTPC_AddPostDataAscii(context->servhandle, context->httphandle, name, value);
+}
+
Result httpcBeginRequest(httpcContext *context)
{
return HTTPC_BeginRequest(context->servhandle, context->httphandle);
return dlret;
}
-Result HTTPC_Initialize(Handle handle)
+Result HTTPC_Initialize(Handle handle, u32 sharedmem_size, Handle sharedmem_handle)
{
u32* cmdbuf=getThreadCommandBuffer();
cmdbuf[0]=IPC_MakeHeader(0x1,1,4); // 0x10044
- cmdbuf[1]=0x1000; // POST buffer size (page aligned)
+ cmdbuf[1]=sharedmem_size; // POST buffer size (page aligned)
cmdbuf[2]=IPC_Desc_CurProcessHandle();
cmdbuf[4]=IPC_Desc_SharedHandles(1);
- cmdbuf[5]=0;// POST buffer memory block handle
+ cmdbuf[5]=sharedmem_handle;// POST buffer memory block handle
Result ret=0;
if(R_FAILED(ret=svcSendSyncRequest(handle)))return ret;
return cmdbuf[1];
}
+Result HTTPC_AddPostDataAscii(Handle handle, Handle contextHandle, char* name, char* value)
+{
+ u32* cmdbuf=getThreadCommandBuffer();
+
+ int name_len=strlen(name)+1;
+ int value_len=strlen(value)+1;
+
+ cmdbuf[0]=IPC_MakeHeader(0x12,3,4); // 0x1200C4
+ cmdbuf[1]=contextHandle;
+ cmdbuf[2]=name_len;
+ cmdbuf[3]=value_len;
+ cmdbuf[4]=IPC_Desc_StaticBuffer(name_len,3);
+ cmdbuf[5]=(u32)name;
+ cmdbuf[6]=IPC_Desc_Buffer(value_len,IPC_BUFFER_R);
+ cmdbuf[7]=(u32)value;
+
+ Result ret=0;
+ if(R_FAILED(ret=svcSendSyncRequest(handle)))return ret;
+
+ return cmdbuf[1];
+}
+
Result HTTPC_BeginRequest(Handle handle, Handle contextHandle)
{
u32* cmdbuf=getThreadCommandBuffer();