diff --git a/configure.py b/configure.py index bb86da5b..01fb5790 100644 --- a/configure.py +++ b/configure.py @@ -1053,19 +1053,19 @@ config.libs = [ ] ), RevoEXLib("nhttp", [ - Object(NonMatching, "nhttp/NHTTP_bgnend.c"), - Object(NonMatching, "nhttp/NHTTP_control.c"), - Object(NonMatching, "nhttp/NHTTP_list.c"), - Object(NonMatching, "nhttp/NHTTP_os_RVL.c"), + Object(Matching, "nhttp/NHTTP_bgnend.c"), + Object(Matching, "nhttp/NHTTP_control.c"), + Object(Matching, "nhttp/NHTTP_list.c"), + Object(Matching, "nhttp/NHTTP_os_RVL.c"), Object(NonMatching, "nhttp/NHTTP_recvbuf.c"), - Object(NonMatching, "nhttp/NHTTP_request.c"), - Object(NonMatching, "nhttp/NHTTP_response.c"), + Object(Matching, "nhttp/NHTTP_request.c"), + Object(Matching, "nhttp/NHTTP_response.c"), Object(NonMatching, "nhttp/NHTTP_socket_RVL.c"), Object(NonMatching, "nhttp/NHTTP_stdlib_RVL.c"), Object(NonMatching, "nhttp/NHTTP_thread.c"), Object(NonMatching, "nhttp/d_nhttp_private.c"), - Object(NonMatching, "nhttp/d_nhttp.c"), - Object(NonMatching, "nhttp/d_nhttp_common.c"), + Object(Matching, "nhttp/d_nhttp.c"), + Object(Matching, "nhttp/d_nhttp_common.c"), ] ), RevoEXLib("nwc24", [ diff --git a/libs/RevoEX/include/private/nhttp.h b/libs/RevoEX/include/private/nhttp.h new file mode 100644 index 00000000..9926ad42 --- /dev/null +++ b/libs/RevoEX/include/private/nhttp.h @@ -0,0 +1,182 @@ +#ifndef PRIVATE_NHTTP_H +#define PRIVATE_NHTTP_H + +#include +#include +#include + +#define STR_POSTBOUND "--t9Sf4yfjf1RtvDu3AA" +#define LEN_POSTBOUND 18 +#define LEN_URLBUF 256 +#define MIN(AA, BB) (((AA) > (BB)) ? (BB) : (AA)) + +typedef struct NHTTPBgnEndInfo NHTTPBgnEndInfo; +typedef struct NHTTPRequestInfo NHTTPRequestInfo; +#define NHTTP_CONN_RECVBUF_SIZE 0x8000 + +typedef struct NHTTPConnectionInfo NHTTPConnectionInfo; + +typedef enum NHTTPConnectionEvent +{ + NHTTP_EVENT_NONE, + NHTTP_EVENT_POST_SEND, + NHTTP_EVENT_BODY_RECV_FULL, + NHTTP_EVENT_BODY_RECV_DONE, + NHTTP_EVENT_COMPLETE, + NHTTP_CALLBACK_EVENT_MAX, +} NHTTPConnectionEvent; + +typedef s32 (*NHTTPConnectionCallback)(NHTTPConnectionInfo* connection, + NHTTPConnectionEvent event, void* arg); + +typedef struct NHTTPConnectionCallbackParam +{ + void* value; + u32 _unk4; + u32 _unk8; + u32 _unkC; +} NHTTPConnectionCallbackParam; + +typedef struct NHTTPPostSendArg +{ + const char* label; + char* buf; + u32 size; + u32 offset; +} NHTTPPostSendArg; + +typedef struct NHTTPBodyBufArg +{ + char* buf; + u32 size; + u32 offset; +} NHTTPBodyBufArg; + +typedef struct NHTTPHeader +{ + struct NHTTPHeader* next; + struct NHTTPHeader* prev; + char* name; + char* value; + u32 length; + u32 _unk14; +} NHTTPHeader; + +typedef struct NHTTPReqQueue +{ + struct NHTTPReqQueue* next; + struct NHTTPReqQueue* prev; + s32 requestId; + NHTTPRequestInfo* volatile request; + s32 _unk10; +} NHTTPReqQueue; + +typedef struct NHTTPListInfo +{ + NHTTPReqQueue* reqQueue; + s32 nextRequestId; +} NHTTPListInfo; + +typedef struct NHTTPReqInfo +{ + NHTTPReqQueue* reqQueue; +} NHTTPReqInfo; + +typedef struct NHTTPi_HDRBUFLIST +{ + struct NHTTPi_HDRBUFLIST* next_p; + u8 block[NHTTP_HDRRECVBUF_BLOCKLEN]; +} NHTTPi_HDRBUFLIST; + +typedef struct NHTTPResponseInfo +{ + volatile s32 headerLen; + volatile s32 bodyLen; + volatile s32 totalBodyLen; + volatile s32 contentLength; + s32 isSuccess; + s32 isHeaderParse; + s32 httpStatus; + u32 recvBufLen; + char* allHeader_p; + char* foundHeader_p; + char* recvBuf_p; + NHTTPResponseCallback bufFull; + NHTTPResponseCleanup freeBuf; + NHTTPi_HDRBUFLIST* hdrBufBlock_p; + u8 hdrBufFirst[NHTTP_HDRRECVBUF_INILEN]; + void* param_p; +} NHTTPResponseInfo; + +struct NHTTPRequestInfo +{ + BOOL cancel; + s32 state; + BOOL secure; + BOOL proxyEnabled; + BOOL isRawData; + s32 hostEnd; + s32 pathStart; + s32 method; + u32 port; + char* url; + char* host; + NHTTPResponseInfo* response; + NHTTPHeader* headers; + NHTTPHeader* postData; + char multipartBoundary[LEN_POSTBOUND + 2]; + char authorization[0x5C]; + s32 authorizationLength; + SSLId sslId; + const char* clientCertData; + u32 clientCertSize; + const char* privateKeyData; + u32 privateKeySize; + const char* rootCAData; + u32 rootCASize; + BOOL clientCertDefault; + u32 verifyOption; + NHTTPEncodingType encodingType; + void* _unkD4; + u32 builtinRootCA; + u32 builtinClientCert; + char proxyServer[0x100]; + u32 proxyPort; + char proxyAuthorization[0x5C]; + s32 proxyAuthorizationLength; + u32 recvBufferSize; + const void* postBuffer; + u32 postBufferSize; + void* _unk250; +}; + +struct NHTTPConnectionInfo +{ + BOOL started; + s32 state; + s32 sslError; + u32 _unkC; + NHTTPRequestInfo* request; + NHTTPResponseInfo* response; + s32 requestId; + NHTTPConnectionCallback callback; + NHTTPConnectionInfo* next; + u32 _unk24; + u32 _unk28; + u32 virtualContentLength; + NHTTPReqCallback requestCallback; + u32 _unk34; + u32 _unk38; + u32 _unk3C; + u8 recvBuf[0x8000]; + u32 recvBufOffset; + u32 recvBufDataLen; + u32 _unk8048; + u32 _unk804C; + u32 _unk8050; + u32 _unk8054; + u32 _unk8058; + u32 _unk805C; +}; + +#endif // PRIVATE_NHTTP_H diff --git a/libs/RevoEX/include/revolution/nhttp.h b/libs/RevoEX/include/revolution/nhttp.h index d94db82c..6fc0bb9d 100644 --- a/libs/RevoEX/include/revolution/nhttp.h +++ b/libs/RevoEX/include/revolution/nhttp.h @@ -1,4 +1,3 @@ - #ifndef REVOLUTION_NHTTP_H #define REVOLUTION_NHTTP_H @@ -6,49 +5,80 @@ #include +#define NHTTP_HDRRECVBUF_INILEN 1024 +#define NHTTP_HDRRECVBUF_BLOCKSHIFT 9 +#define NHTTP_HDRRECVBUF_BLOCKLEN (1 << NHTTP_HDRRECVBUF_BLOCKSHIFT) +#define NHTTP_HDRRECVBUF_BLOCKMASK (NHTTP_HDRRECVBUF_BLOCKLEN - 1) + #ifdef __cplusplus extern "C" { #endif // __cplusplus -typedef struct { - u8 dummy[1]; -} NHTTPRequest; -typedef struct { - u8 dummy[1]; -} NHTTPResponse; - -typedef int NHTTPConnection; - typedef void* (*NHTTPAlloc)(u32 size, int align); typedef void (*NHTTPFree)(void* buf); - -typedef void (*NHTTPRequestCallback)(int, NHTTPResponse*); typedef void (*NHTTPCleanupCallback)(); -int NHTTPStartup(NHTTPAlloc alloc, NHTTPFree free, u32 priority); -int NHTTPCleanupAsync(NHTTPCleanupCallback); -int NHTTPGetError(); +typedef struct NHTTPRequestInfo NHTTPRequest; +typedef struct NHTTPResponseInfo NHTTPResponse; +typedef s32 NHTTPConnection; -NHTTPRequest* NHTTPCreateRequest(char* url, undefined4, void* buf, u32 bufSize, NHTTPRequestCallback reqCallback, undefined4); -NHTTPRequest* NHTTPCreateRequestEx(char* url, undefined4, void* buf, u32 bufSize, NHTTPRequestCallback reqCallback, undefined4, undefined4, - undefined4); -int NHTTPDeleteRequest(NHTTPRequest* request); +typedef enum NHTTPReqMethod { + NHTTP_REQMETHOD_GET, + NHTTP_REQMETHOD_POST, + NHTTP_REQMETHOD_HEAD, +} NHTTPReqMethod; + +typedef enum NHTTPEncodingType { + NHTTP_ENCODING_TYPE_AUTO, + NHTTP_ENCODING_TYPE_URL, + NHTTP_ENCODING_TYPE_MULTIPART, + NHTTP_ENCODING_TYPE_MAX, +} NHTTPEncodingType; + +typedef void (*NHTTPReqCallback)(); +typedef void* (*NHTTPResponseCallback)(void** buffer, void* data, + u32 contentLength, NHTTPAlloc alloc, + NHTTPFree free, void* userParam); +typedef void (*NHTTPResponseCleanup)(void* buffer, NHTTPFree free, + void* userParam); + +s32 NHTTPStartup(NHTTPAlloc alloc, NHTTPFree free, u32 priority); +void NHTTPCleanupAsync(NHTTPCleanupCallback callback); +s32 NHTTPGetError(void); + +NHTTPRequest* NHTTPCreateRequest(const char* url, NHTTPReqMethod method, + void* buffer, u32 bufferSize, + NHTTPReqCallback callback, void* userParam); +NHTTPRequest* NHTTPCreateRequestEx(const char* url, NHTTPReqMethod method, + void* buffer, u32 bufferSize, + NHTTPReqCallback callback, void* userParam, + NHTTPResponseCallback responseCallback, + NHTTPResponseCleanup cleanup); +s32 NHTTPAddHeaderField(NHTTPRequest* request, const char* name, + const char* value); +s32 NHTTPAddPostDataRaw(NHTTPRequest* request, const void* data, + u32 size, const char* label); +s32 NHTTPDeleteRequest(NHTTPRequest* request); NHTTPConnection NHTTPSendRequestAsync(NHTTPRequest* request); -int NHTTPCancelRequestAsync(NHTTPConnection connection); +s32 NHTTPCancelRequestAsync(NHTTPConnection connection); -void NHTTPGetProgress(u32* complete, u32* total); +s32 NHTTPGetProgress(u32* complete, u32* total); -int NHTTPDestroyResponse(NHTTPResponse* response); -u32 NHTTPGetHeaderField(NHTTPResponse* response, const char* name, const char** value); +void NHTTPDestroyResponse(NHTTPResponse* response); +s32 NHTTPGetHeaderAll(NHTTPResponse* response, const char** header); +s32 NHTTPGetHeaderField(NHTTPResponse* response, const char* name, + const char** value); -int NHTTPGetResultCode(NHTTPResponse* res); +s32 NHTTPGetBodyAll(NHTTPResponse* response, void** buffer); +s32 NHTTPGetResultCode(NHTTPResponse* response); -int NHTTPSetVerifyOption(NHTTPRequest* request, u32 option); - -int NHTTPSetProxyDefault(NHTTPRequest* request); -int NHTTPSetRootCADefault(NHTTPRequest* request); -int NHTTPSetClientCertDefault(NHTTPRequest* request); +s32 NHTTPSetVerifyOption(NHTTPRequest* request, u32 option); +s32 NHTTPSetProxy(NHTTPRequest* request, const char* server, u16 port, + const char* username, const char* password); +s32 NHTTPSetProxyDefault(NHTTPRequest* request); +s32 NHTTPSetClientCertDefault(NHTTPRequest* request); +s32 NHTTPSetRootCADefault(NHTTPRequest* request); #ifdef __cplusplus } diff --git a/libs/RevoEX/include/revolution/so/SOBasic.h b/libs/RevoEX/include/revolution/so/SOBasic.h index b79a96bd..54ef91ee 100644 --- a/libs/RevoEX/include/revolution/so/SOBasic.h +++ b/libs/RevoEX/include/revolution/so/SOBasic.h @@ -102,13 +102,16 @@ struct SOAddrInfo { SOAddrInfo* next; // 0x20 }; +#define SO_ENOTCONN -56 + int SOSocket(int pf, int type, int protocol); +int __SOCreateSocket(int pf, int type, int protocol); int SOClose(int s); int SOConnect(int s, void* sockAddr); int SORecv(int s, void* buf, int len, int flags); -int SOSend(int s, void* buf, int len, int flags); +int SOSend(int s, const void* buf, int len, int flags); int SOFcntl(int s, int cmd, ...); int SOShutdown(int s, int how); diff --git a/libs/RevoEX/include/revolution/so/SOOption.h b/libs/RevoEX/include/revolution/so/SOOption.h index 76e1fa64..df35c735 100644 --- a/libs/RevoEX/include/revolution/so/SOOption.h +++ b/libs/RevoEX/include/revolution/so/SOOption.h @@ -14,6 +14,7 @@ typedef struct IPAddrEntry { } IPAddrEntry; int SOGetInterfaceOpt(void* unk, int level, int optname, void* optval, int* optlen); +int SOSetSockOpt(int s, int level, int optname, const void* optval, int optlen); #ifdef __cplusplus } diff --git a/libs/RevoEX/src/nhttp/NHTTP_bgnend.c b/libs/RevoEX/src/nhttp/NHTTP_bgnend.c new file mode 100644 index 00000000..85588e15 --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_bgnend.c @@ -0,0 +1,179 @@ +#include + +#include +#include + +#include + +int SOClose(int s); + +typedef struct NHTTPBgnEndInfo +{ + NCDIpConfig ipConfig; + NHTTPAlloc alloc; + NHTTPFree free; + BOOL started; + s32 socket; + s32 sslError; + NHTTPErr error; + u32 _unk7DC; + void* threadStack; + u32 _unk7E4; +} NHTTPBgnEndInfo; + +NHTTPBgnEndInfo* NHTTPi_GetSystemInfoP(void); +NHTTPBgnEndInfo* NHTTPi_GetBgnEndInfoP(void* systemInfo); +void* NHTTPi_GetListInfoP(void* systemInfo); +void* NHTTPi_GetReqInfoP(void* systemInfo); +void* NHTTPi_GetMutexInfoP(void* systemInfo); +void* NHTTPi_GetThreadInfoP(void* systemInfo); + +void NHTTPi_InitListInfo(void* listInfo); +void NHTTPi_InitRequestInfo(void* reqInfo); +void NHTTPi_initLockReqList(void* mutexInfo); +void NHTTPi_exitLockReqList(void); +BOOL NHTTPi_createCommThread(void* threadInfo, u32 priority, void* stack); +void NHTTPi_destroyCommThread(void* threadInfo, NHTTPBgnEndInfo* bgnEndInfo); +void NHTTPi_CheckCurrentThread(void* threadInfo, BOOL allowCommThread); +void NHTTPi_cancelAllRequests(void* systemInfo); +int NHTTPi_GetConnectionListLength(void); +void NHTTPi_InitConnectionList(void); + +void NHTTPi_InitBgnEndInfo(NHTTPBgnEndInfo* info) +{ + info->sslError = 0; + info->error = NHTTP_ERROR_NONE; + info->alloc = NULL; + info->free = NULL; + info->started = FALSE; + info->socket = -1; + info->_unk7DC = 0; + info->threadStack = NULL; + info->_unk7E4 = 0; +} + +void* NHTTPi_alloc(u32 size, int align) +{ + NHTTPBgnEndInfo* info = NHTTPi_GetSystemInfoP(); + + if (info->alloc != NULL) + { + return info->alloc(size, align); + } + return NULL; +} + +void NHTTPi_free(void* buf) +{ + NHTTPBgnEndInfo* info = NHTTPi_GetSystemInfoP(); + + if (info->free != NULL) + { + info->free(buf); + } +} + +void NHTTPi_SetError(NHTTPBgnEndInfo* info, NHTTPErr error) +{ + info->error = error; +} + +void NHTTPi_SetSSLError(NHTTPBgnEndInfo* info, s32 error) +{ + info->sslError = error; +} + +s32 NHTTPi_GetSSLError(NHTTPBgnEndInfo* info) +{ + return info->sslError; +} + +BOOL NHTTPi_Startup(void* systemInfo, NHTTPAlloc alloc, NHTTPFree free, + u32 priority) +{ + NHTTPBgnEndInfo* bgnEndInfo = NHTTPi_GetBgnEndInfoP(systemInfo); + void* listInfo = NHTTPi_GetListInfoP(systemInfo); + void* reqInfo = NHTTPi_GetReqInfoP(systemInfo); + void* mutexInfo = NHTTPi_GetMutexInfoP(systemInfo); + void* threadInfo = NHTTPi_GetThreadInfoP(systemInfo); + void* stack; + NCDErr result; + + bgnEndInfo->alloc = alloc; + bgnEndInfo->free = free; + bgnEndInfo->error = NHTTP_ERROR_NONE; + bgnEndInfo->sslError = 0; + bgnEndInfo->_unk7DC = 0; + + NHTTPi_InitListInfo(listInfo); + NHTTPi_InitRequestInfo(reqInfo); + NHTTPi_initLockReqList(mutexInfo); + NHTTPi_InitConnectionList(); + bgnEndInfo->socket = -1; + + stack = NHTTPi_alloc(0x2000, 8); + bgnEndInfo->threadStack = stack; + if (stack == NULL) + { + bgnEndInfo->error = NHTTP_ERROR_ALLOC; + NHTTPi_exitLockReqList(); + return FALSE; + } + + if (!NHTTPi_createCommThread(threadInfo, priority, stack)) + { + bgnEndInfo->error = NHTTP_ERROR_REVOLUTIONSDK; + NHTTPi_free(bgnEndInfo->threadStack); + bgnEndInfo->threadStack = NULL; + NHTTPi_exitLockReqList(); + return FALSE; + } + + result = NCDGetCurrentIpConfig(&bgnEndInfo->ipConfig); + if (result < 0) + { + OSReport("NCDGetCurrentIpConfig err = %d\n", result); + OSPanic("NHTTP_bgnend.c", 230, "NCDGetCurrentIpConfig"); + } + + bgnEndInfo->started = TRUE; + return TRUE; +} + +void NHTTPi_CleanupAsync(void* systemInfo, NHTTPCleanupCallback callback) +{ + NHTTPBgnEndInfo* bgnEndInfo = NHTTPi_GetBgnEndInfoP(systemInfo); + void* threadInfo = NHTTPi_GetThreadInfoP(systemInfo); + int connectionCount; + + NHTTPi_CheckCurrentThread(threadInfo, TRUE); + NHTTPi_cancelAllRequests(systemInfo); + NHTTPi_destroyCommThread(threadInfo, bgnEndInfo); + NHTTPi_free(bgnEndInfo->threadStack); + bgnEndInfo->threadStack = NULL; + NHTTPi_exitLockReqList(); + bgnEndInfo->started = FALSE; + + if (callback != NULL) + { + callback(); + } + + connectionCount = NHTTPi_GetConnectionListLength(); + if (connectionCount != 0) + { + printf("*warning: %d connections rests! Please free connections.\n", + connectionCount); + } + + if (bgnEndInfo->socket >= 0) + { + SOClose(bgnEndInfo->socket); + bgnEndInfo->socket = -1; + } +} + +s32 NHTTPi_GetError(NHTTPBgnEndInfo* info) +{ + return info->error; +} diff --git a/libs/RevoEX/src/nhttp/NHTTP_control.c b/libs/RevoEX/src/nhttp/NHTTP_control.c new file mode 100644 index 00000000..adc5225f --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_control.c @@ -0,0 +1,102 @@ +#include + +#include +s32 NHTTPi_compareToken(const char* lhs, const char* rhs); +void* NHTTPi_alloc(u32 size, int align); +void NHTTPi_SetError(NHTTPBgnEndInfo* info, NHTTPErr error); + +static BOOL addHdrList(NHTTPHeader** list, NHTTPBgnEndInfo* info, char* name, + char* value) +{ + BOOL found = FALSE; + NHTTPHeader* header = *list; + + if (header != NULL) + { + if (NHTTPi_compareToken(name, header->name) != 0) + { + header = header->prev; + while (header != *list) + { + if (NHTTPi_compareToken(name, header->name) == 0) + { + found = TRUE; + break; + } + header = header->prev; + } + } + else + { + found = TRUE; + } + } + + if (found) + { + header->value = value; + } + else + { + header = NHTTPi_alloc(sizeof(NHTTPHeader), 4); + if (header == NULL) + { + printf("failed to allocate memory\n"); + NHTTPi_SetError(info, NHTTP_ERROR_ALLOC); + return FALSE; + } + + header->name = name; + header->value = value; + header->length = 0; + header->_unk14 = 0; + + if (*list != NULL) + { + header->next = (*list)->next; + header->prev = *list; + (*list)->next->prev = header; + (*list)->next = header; + } + else + { + header->prev = header; + header->next = header; + *list = header; + } + } + + return TRUE; +} + +NHTTPHeader* NHTTPi_getHdrFromList(NHTTPHeader** list) +{ + NHTTPHeader* header = *list; + + if (header != NULL) + { + if (header != header->next) + { + header->next->prev = header->prev; + header->prev->next = header->next; + *list = header->prev; + } + else + { + *list = NULL; + } + } + + return header; +} + +BOOL NHTTP_AddHeaderField(NHTTPRequestInfo* request, NHTTPBgnEndInfo* info, + char* name, char* value) +{ + if (request->state != 0) + { + return FALSE; + } + return addHdrList(&request->headers, info, name, value); +} + diff --git a/libs/RevoEX/src/nhttp/NHTTP_list.c b/libs/RevoEX/src/nhttp/NHTTP_list.c new file mode 100644 index 00000000..2a600916 --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_list.c @@ -0,0 +1,126 @@ +#include + +void* NHTTPi_alloc(u32 size, int align); +void NHTTPi_free(void* ptr); +NHTTPConnectionInfo* NHTTPi_Request2Connection(void* mutexInfo, + NHTTPRequestInfo* request); +void NHTTPi_destroyRequestObject(void* mutexInfo, NHTTPRequestInfo* request); +void NHTTPi_CompleteCallback(void* mutexInfo, NHTTPConnectionInfo* connection); +NHTTPHeader* NHTTPi_getHdrFromList(NHTTPHeader** list); + +void NHTTPi_InitListInfo(NHTTPListInfo* info) +{ + info->reqQueue = NULL; + info->nextRequestId = 0; +} + +s32 NHTTPi_setReqQueue(NHTTPListInfo* info, NHTTPRequestInfo* request) +{ + s32 requestId = -1; + NHTTPReqQueue* queue = NHTTPi_alloc(sizeof(NHTTPReqQueue), 4); + + if (queue != NULL) + { + if (info->reqQueue != NULL) + { + queue->next = info->reqQueue->next; + queue->prev = info->reqQueue; + info->reqQueue->next->prev = queue; + info->reqQueue->next = queue; + } + else + { + queue->next = queue; + queue->prev = queue; + info->reqQueue = queue; + } + + queue->requestId = info->nextRequestId++; + queue->request = request; + queue->_unk10 = -1; + requestId = queue->requestId; + + if (info->nextRequestId < 0) + { + info->nextRequestId = 0; + } + } + + return requestId; +} + +BOOL NHTTPi_freeReqQueue(NHTTPListInfo* info, void* mutexInfo, s32 requestId) +{ + NHTTPReqQueue* current; + NHTTPReqQueue* queue = info->reqQueue; + NHTTPReqQueue* found = NULL; + BOOL result = FALSE; + + if (queue != NULL) + { + if (queue->requestId == requestId) + { + found = queue; + } + else + { + current = queue->prev; + + while (current != queue) + { + if (current->requestId == requestId) + { + found = current; + break; + } + current = current->prev; + } + } + } + + if (found != NULL) + { + NHTTPConnectionInfo* connection; + + if (queue != queue->next) + { + found->next->prev = found->prev; + found->prev->next = found->next; + + if (info->reqQueue == found) + { + info->reqQueue = found->prev; + } + } + else + { + info->reqQueue = NULL; + } + + connection = NHTTPi_Request2Connection(mutexInfo, found->request); + NHTTPi_destroyRequestObject(mutexInfo, found->request); + NHTTPi_free(found); + + if (connection != NULL) + { + connection->state = 8; + NHTTPi_CompleteCallback(mutexInfo, connection); + } + result = TRUE; + } + + return result; +} + +void NHTTPi_allFreeReqQueue(NHTTPListInfo* info, void* mutexInfo) +{ + while (info->reqQueue != NULL) + { + NHTTPi_freeReqQueue(info, mutexInfo, info->reqQueue->requestId); + } +} + +NHTTPReqQueue* NHTTPi_getReqFromReqQueue(NHTTPReqQueue** queue) +{ + return (NHTTPReqQueue*)NHTTPi_getHdrFromList((NHTTPHeader**)queue); +} diff --git a/libs/RevoEX/src/nhttp/NHTTP_os_RVL.c b/libs/RevoEX/src/nhttp/NHTTP_os_RVL.c new file mode 100644 index 00000000..69c96274 --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_os_RVL.c @@ -0,0 +1,120 @@ +#include + +#include +#include + +typedef struct NHTTPMutexInfo +{ + OSMutex mutex; + BOOL initialized; +} NHTTPMutexInfo; + +typedef struct NHTTPBgnEndInfo +{ + NCDIpConfig ipConfig; + NHTTPAlloc alloc; + NHTTPFree free; + BOOL started; + s32 socket; + s32 sslError; + NHTTPErr error; + u32 _unk7DC; + void* threadStack; + u32 _unk7E4; +} NHTTPBgnEndInfo; + +typedef struct NHTTPThreadInfo +{ + OSMessageQueue messageQueue; + OSMessage messages[3]; + OSThread thread; + BOOL isCreateCommThreadMessageQueue; + u8 _unk34C[0x14]; + char commBuf[0x100]; +} NHTTPThreadInfo; + +BOOL NHTTPi_IsCreateCommThreadMessageQueue(NHTTPThreadInfo* info); +void NHTTPi_IsCreateCommThreadMessageQueueOn(NHTTPThreadInfo* info); +void NHTTPi_CommThreadProcMain(void* argument); + +void NHTTPi_InitMutexInfo(NHTTPMutexInfo* info) +{ + info->initialized = FALSE; +} + +void NHTTPi_initLockReqList(NHTTPMutexInfo* info) +{ + if (!info->initialized) + { + OSInitMutex(&info->mutex); + info->initialized = TRUE; + } +} + +void NHTTPi_exitLockReqList(void) +{ +} + +void NHTTPi_lockReqList(NHTTPMutexInfo* info) +{ + OSLockMutex(&info->mutex); +} + +void NHTTPi_unlockReqList(NHTTPMutexInfo* info) +{ + OSUnlockMutex(&info->mutex); +} + +void* NHTTPi_CommThreadProc(void* argument); + +BOOL NHTTPi_createCommThread(NHTTPThreadInfo* info, s32 priority, void* stack) +{ + if (!NHTTPi_IsCreateCommThreadMessageQueue(info)) + { + OSInitMessageQueue(&info->messageQueue, info->messages, 3); + NHTTPi_IsCreateCommThreadMessageQueueOn(info); + } + + OSCreateThread(&info->thread, NHTTPi_CommThreadProc, NULL, (u8*)stack + 0x2000, 0x2000, priority, 0); + OSResumeThread(&info->thread); + return TRUE; +} + +void NHTTPi_destroyCommThread(NHTTPThreadInfo* info, NHTTPBgnEndInfo* bgnEndInfo) +{ + bgnEndInfo->_unk7DC = 1; + OSSendMessage(&info->messageQueue, NULL, 0); + OSJoinThread(&info->thread, NULL); +} + +void NHTTPi_idleCommThread(NHTTPThreadInfo* info) +{ + OSMessage message; + + OSReceiveMessage(&info->messageQueue, &message, OS_MESSAGE_BLOCK); +} + +void NHTTPi_kickCommThread(NHTTPThreadInfo* info) +{ + OSSendMessage(&info->messageQueue, NULL, 0); +} + +void NHTTPi_CheckCurrentThread(NHTTPThreadInfo* info, BOOL notCommThread) +{ + OSThread* current = OSGetCurrentThread(); + OSThread* commThread = &info->thread; + + if (current != NULL + && ((!notCommThread && current != commThread) + || (notCommThread && current == commThread))) + { + OSReport("%s:illegal thread\n", __FUNCTION__); + OSPanic(__FILE__, 223, "halt\n"); + } +} + +void* NHTTPi_CommThreadProc(void* argument) +{ + NHTTPi_CommThreadProcMain(argument); + return NULL; +} diff --git a/libs/RevoEX/src/nhttp/NHTTP_request.c b/libs/RevoEX/src/nhttp/NHTTP_request.c new file mode 100644 index 00000000..cba4b038 --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_request.c @@ -0,0 +1,487 @@ +#include + +const char NHTTPi_strMultipartBound[] = STR_POSTBOUND; + +void* NHTTPi_alloc(u32 size, int align); +void NHTTPi_free(void* ptr); +void NHTTPi_SetError(NHTTPBgnEndInfo* info, NHTTPErr error); +void* NHTTPi_memclr(void* destination, u32 size); +s32 NHTTPi_strlen(const char* string); +void* NHTTPi_memcpy(void* destination, const void* source, u32 size); +s32 NHTTPi_strnicmp(const char* left, const char* right, s32 size); +s32 NHTTPi_strToHex(const char* string, s32 length); +s32 NHTTPi_strtonum(const char* string, s32 length); + +NHTTPBgnEndInfo* NHTTPi_GetBgnEndInfoP(void* systemInfo); +NHTTPListInfo* NHTTPi_GetListInfoP(void* systemInfo); +NHTTPReqInfo* NHTTPi_GetReqInfoP(void* systemInfo); +void* NHTTPi_GetMutexInfoP(void* systemInfo); +void* NHTTPi_GetThreadInfoP(void* systemInfo); +NHTTPConnectionInfo* NHTTPi_Request2Connection(void* mutexInfo, + NHTTPRequestInfo* request); +void NHTTPi_lockReqList(void* mutexInfo); +void NHTTPi_unlockReqList(void* mutexInfo); +s32 NHTTPi_setReqQueue(NHTTPListInfo* listInfo, NHTTPRequestInfo* request); +void NHTTPi_kickCommThread(void* threadInfo); +void NHTTPi_SocCancel(void* mutexInfo, NHTTPRequestInfo* request, s32 socket); +BOOL NHTTPi_freeReqQueue(NHTTPListInfo* listInfo, void* mutexInfo, + s32 requestId); +void NHTTPi_allFreeReqQueue(NHTTPListInfo* listInfo, void* mutexInfo); + +void NHTTPi_InitRequestInfo(NHTTPReqInfo* info) +{ + info->reqQueue = NULL; +} + +NHTTPRequestInfo* NHTTP_CreateRequest(NHTTPBgnEndInfo* bgnEndInfo_p, + const char* url_p, s32 method, void* buf_p, u32 len, void* param_p, + NHTTPResponseCallback bufFull, NHTTPResponseCleanup freeBuf) +{ + int i; + int suflen, urllen, prelen; + int nhostenc, deccount; + int declen, hostend; + int port; + int decode_urllen; + + char a_url[LEN_URLBUF]; + char* prefix_p; + NHTTPRequestInfo* req_p = NULL; + + if (method >= 3 || method < 0) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_UNKNOWN); + goto errend; + } + + req_p = NHTTPi_alloc(sizeof(NHTTPRequestInfo), 4); + if (!req_p) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_ALLOC); + goto errend; + } + NHTTPi_memclr(req_p, sizeof(NHTTPRequestInfo)); + + req_p->response = NHTTPi_alloc(sizeof(NHTTPResponseInfo), 4); + if (!req_p->response) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_ALLOC); + goto errend; + } + NHTTPi_memclr(req_p->response, sizeof(NHTTPResponseInfo)); + req_p->response->recvBuf_p = buf_p; + req_p->response->recvBufLen = len; + req_p->response->bufFull = bufFull; + req_p->response->freeBuf = freeBuf; + + urllen = NHTTPi_strlen(url_p); + if (urllen <= 7) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_DNS); + goto errend; + } + + NHTTPi_memclr(a_url, urllen); + NHTTPi_memcpy(a_url, url_p, urllen); + + req_p->port = 80; + suflen = 7; + if (NHTTPi_strnicmp(a_url, "http://", 7) != 0) + { + if (NHTTPi_strnicmp(a_url, "https://", 8) != 0) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_DNS); + goto errend; + } + req_p->secure = TRUE; + req_p->port = 443; + suflen = 8; + } + + prefix_p = a_url + suflen; + prelen = urllen - suflen; + if (prelen <= 0) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_DNS); + goto errend; + } + + for (i = 0, nhostenc = 0, deccount = 0; + (i < prelen) && (prefix_p[i] != '/'); i++) + { + if (deccount == 2) + { + deccount--; + } + else if (deccount == 1) + { + s8 c = (s8)NHTTPi_strToHex(&prefix_p[i - 1], 2); + deccount--; + if (c < 0) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_DNS); + goto errend; + } + if (c == '/') + { + nhostenc--; + break; + } + } + else if (prefix_p[i] == '%') + { + deccount = 2; + nhostenc++; + } + } + + if (deccount) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_DNS); + goto errend; + } + + decode_urllen = suflen + prelen - nhostenc * 2 + 1; + req_p->url = NHTTPi_alloc(decode_urllen, 4); + if (!req_p->url) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_ALLOC); + goto errend; + } + NHTTPi_memclr(req_p->url, decode_urllen); + NHTTPi_memcpy(req_p->url, a_url, suflen); + + for (i = 0, declen = 0, deccount = 0, hostend = FALSE; + i < prelen; i++) + { + if (deccount == 2) + { + deccount--; + } + else if (deccount == 1) + { + char c = (char)NHTTPi_strToHex(&prefix_p[i - 1], 2); + deccount--; + req_p->url[suflen + declen - 1] = c; + if (c == '/') + { + hostend = TRUE; + } + } + else + { + if (prefix_p[i] == '/') + { + hostend = TRUE; + } + if (!hostend & (prefix_p[i] == '%')) + { + deccount = 2; + } + else + { + req_p->url[suflen + declen] = prefix_p[i]; + } + declen++; + } + } + req_p->url[suflen + declen] = 0; + + prefix_p = &req_p->url[suflen]; + prelen = declen; + + for (i = 0; i < prelen; i++) + { + if ((prefix_p[i] == '/') || (prefix_p[i] == ':')) + { + req_p->hostEnd = i + suflen; + break; + } + } + + if (i == prelen) + { + req_p->pathStart = req_p->hostEnd = i + suflen; + } + else if (prefix_p[i] == '/') + { + req_p->pathStart = req_p->hostEnd; + } + else if (prefix_p[i] == ':') + { + for (; i < prelen; i++) + { + if (prefix_p[i] == '/') + { + req_p->pathStart = i + suflen; + break; + } + } + + if (i == prelen) + { + req_p->pathStart = i + suflen; + } + else + { + port = NHTTPi_strtonum(&req_p->url[req_p->hostEnd + 1], + (int)(req_p->pathStart - (req_p->hostEnd + 1))); + if (port < 0) + { + port = req_p->port; + } + else if (port > 65535) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_DNS); + goto errend; + } + req_p->port = (u16)port; + } + } + + { + u32 a_hostnameLen = (u32)(req_p->hostEnd - (req_p->secure ? 8 : 7)); + req_p->host = NHTTPi_alloc(a_hostnameLen + 1, 4); + if (!req_p->host) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_ALLOC); + goto errend; + } + NHTTPi_memclr(req_p->host, (int)(a_hostnameLen + 1)); + NHTTPi_memcpy(req_p->host, + req_p->url + (req_p->secure ? 8 : 7), (int)a_hostnameLen); + } + + NHTTPi_memcpy(req_p->multipartBoundary, NHTTPi_strMultipartBound, + sizeof(req_p->multipartBoundary)); + req_p->method = method; + req_p->sslId = 0; + req_p->clientCertData = NULL; + req_p->clientCertSize = 0; + req_p->privateKeyData = NULL; + req_p->privateKeySize = 0; + req_p->rootCAData = NULL; + req_p->rootCASize = 0; + req_p->clientCertDefault = FALSE; + req_p->verifyOption = 0; + req_p->_unkD4 = NULL; + req_p->response->param_p = param_p; + req_p->proxyEnabled = FALSE; + req_p->recvBufferSize = 0; + return req_p; + +errend: + if (req_p) + { + if (req_p->url) + { + NHTTPi_free(req_p->url); + } + if (req_p->host) + { + NHTTPi_free(req_p->host); + } + if (req_p->response) + { + NHTTPi_free(req_p->response); + } + NHTTPi_free(req_p); + } + return NULL; +} + +void NHTTP_DestroyRequest(void* systemInfo, NHTTPRequestInfo* request) +{ + NHTTPConnectionInfo* connection; + NHTTPHeader* header; + NHTTPHeader* next; + NHTTPHeader* post; + NHTTPHeader* nextPost; + void* mutexInfo = NHTTPi_GetMutexInfoP(systemInfo); + + connection = NHTTPi_Request2Connection(mutexInfo, request); + if (connection != NULL) + { + connection->response = NULL; + } + NHTTPi_free(request->response); + + connection = NHTTPi_Request2Connection(mutexInfo, request); + if (connection != NULL) + { + connection->request = NULL; + } + + header = request->headers; + while (header != NULL) + { + if (header != header->next) + { + next = header->next->next; + NHTTPi_free(header->next); + header->next = next; + } + else + { + NHTTPi_free(header); + header = NULL; + } + } + + post = request->postData; + while (post != NULL) + { + if (post != post->next) + { + nextPost = post->next->next; + NHTTPi_free(post->next); + post->next = nextPost; + } + else + { + NHTTPi_free(post); + post = NULL; + } + } + + NHTTPi_free(request->url); + NHTTPi_free(request->host); + NHTTPi_free(request); +} + +BOOL NHTTPi_destroyRequestObject(void* mutexInfo, NHTTPRequestInfo* request) +{ + NHTTPConnectionInfo* connection; + NHTTPHeader* header; + NHTTPHeader* next; + NHTTPHeader* post; + NHTTPHeader* nextPost; + + connection = NHTTPi_Request2Connection(mutexInfo, request); + if (connection != NULL) + { + connection->request = NULL; + } + + header = request->headers; + while (header != NULL) + { + if (header != header->next) + { + next = header->next->next; + NHTTPi_free(header->next); + header->next = next; + } + else + { + NHTTPi_free(header); + header = NULL; + } + } + + post = request->postData; + while (post != NULL) + { + if (post != post->next) + { + nextPost = post->next->next; + NHTTPi_free(post->next); + post->next = nextPost; + } + else + { + NHTTPi_free(post); + post = NULL; + } + } + + NHTTPi_free(request->url); + NHTTPi_free(request->host); + NHTTPi_free(request); + return TRUE; +} + +s32 NHTTP_SendRequestAsync(void* systemInfo, NHTTPRequestInfo* request) +{ + NHTTPBgnEndInfo* info = NHTTPi_GetBgnEndInfoP(systemInfo); + void* threadInfo = NHTTPi_GetThreadInfoP(systemInfo); + void* mutexInfo = NHTTPi_GetMutexInfoP(systemInfo); + s32 requestId; + + if (request->state != 0) + { + NHTTPi_SetError(info, NHTTP_ERROR_UNKNOWN); + return -1; + } + + NHTTPi_lockReqList(mutexInfo); + requestId = NHTTPi_setReqQueue(NHTTPi_GetListInfoP(systemInfo), request); + if (requestId >= 0) + { + request->state = 1; + NHTTPi_kickCommThread(threadInfo); + } + else + { + NHTTPi_SetError(info, NHTTP_ERROR_ALLOC); + } + NHTTPi_unlockReqList(mutexInfo); + return requestId; +} + +BOOL NHTTP_CancelRequestAsync(void* sysInfo_p, s32 id) +{ + BOOL rc = FALSE; + NHTTPReqInfo* reqInfo_p = NHTTPi_GetReqInfoP(sysInfo_p); + void* mutexInfo_p = NHTTPi_GetMutexInfoP(sysInfo_p); + volatile NHTTPReqQueue* reqCurrent_p = reqInfo_p->reqQueue; + + NHTTPi_lockReqList(mutexInfo_p); + + if (reqCurrent_p) + { + if (reqCurrent_p->requestId == id) + { + if (!reqCurrent_p->request->cancel) + { + reqCurrent_p->request->cancel = TRUE; + NHTTPi_SocCancel(mutexInfo_p, reqCurrent_p->request, + reqCurrent_p->_unk10); + rc = TRUE; + } + } + } + + if (!rc) + { + NHTTPListInfo* listInfo_p = NHTTPi_GetListInfoP(sysInfo_p); + rc = NHTTPi_freeReqQueue(listInfo_p, mutexInfo_p, id); + } + + NHTTPi_unlockReqList(mutexInfo_p); + + return rc; +} + +void NHTTPi_cancelAllRequests(void* sysInfo_p) +{ + NHTTPReqInfo* reqInfo_p = NHTTPi_GetReqInfoP(sysInfo_p); + NHTTPListInfo* listInfo_p = NHTTPi_GetListInfoP(sysInfo_p); + void* mutexInfo_p = NHTTPi_GetMutexInfoP(sysInfo_p); + volatile NHTTPReqQueue* reqCurrent_p = reqInfo_p->reqQueue; + + NHTTPi_lockReqList(mutexInfo_p); + + if (reqCurrent_p) + { + if (!reqCurrent_p->request->cancel) + { + reqCurrent_p->request->cancel = TRUE; + NHTTPi_SocCancel(mutexInfo_p, reqCurrent_p->request, + reqCurrent_p->_unk10); + } + } + + NHTTPi_allFreeReqQueue(listInfo_p, mutexInfo_p); + NHTTPi_unlockReqList(mutexInfo_p); +} diff --git a/libs/RevoEX/src/nhttp/NHTTP_response.c b/libs/RevoEX/src/nhttp/NHTTP_response.c new file mode 100644 index 00000000..de023bc0 --- /dev/null +++ b/libs/RevoEX/src/nhttp/NHTTP_response.c @@ -0,0 +1,202 @@ +#include + +void* NHTTPi_alloc(u32 size, int align); +void NHTTPi_free(void* ptr); +void NHTTPi_SetError(NHTTPBgnEndInfo* info, NHTTPErr error); +s32 NHTTPi_loadFromHdrRecvBuf(NHTTPResponseInfo* res_p, char* dst, s32 pos, + s32 size); +s32 NHTTPi_strcmp(const char* lhs, const char* rhs); +s32 NHTTPi_getHeaderValue(const NHTTPResponseInfo* res_p, const char* label_p, + s32* pos_p); +NHTTPConnectionInfo* NHTTPi_Response2Connection(void* mutexInfo_p, + NHTTPResponseInfo* res_p); +s32 NHTTPi_findNextLineHdrRecvBuf(const NHTTPResponseInfo* res_p, s32 pos, + s32 limit, s32* colon_pos_p, s32* return_code_size_p); +s32 NHTTPi_compareTokenN_HdrRecvBuf(const NHTTPResponseInfo* res_p, s32 pos, + s32 limit, const char* str_p, s8 extdeli); +s32 NHTTPi_skipSpaceHdrRecvBuf( + const NHTTPResponseInfo* res_p, s32 pos, s32 limit); + +void NHTTP_DestroyResponse(void* mutexInfo_p, NHTTPResponseInfo* res_p) +{ + NHTTPi_HDRBUFLIST* list_p = NULL; + + while (res_p->hdrBufBlock_p) + { + list_p = res_p->hdrBufBlock_p->next_p; + NHTTPi_free(res_p->hdrBufBlock_p); + res_p->hdrBufBlock_p = list_p; + } + + if (res_p->allHeader_p) + { + NHTTPi_free(res_p->allHeader_p); + } + + if (res_p->foundHeader_p) + { + NHTTPi_free(res_p->foundHeader_p); + } + + if (res_p->freeBuf != NULL) + { + res_p->freeBuf(res_p->recvBuf_p, NHTTPi_free, res_p->param_p); + res_p->recvBuf_p = NULL; + res_p->recvBufLen = 0; + } + + { + NHTTPConnectionInfo* connection_p = + NHTTPi_Response2Connection(mutexInfo_p, res_p); + + if (connection_p != NULL) + { + connection_p->response = NULL; + } + } + + NHTTPi_free(res_p); +} + +s32 NHTTPi_getHeaderValue(const NHTTPResponseInfo* res_p, const char* label_p, + s32* pos_p) +{ + s32 linetop, nextlinetop; + s32 labeltop, labelend; + s32 valuetop, valueend; + s32 return_code_size = 0; + + linetop = NHTTPi_findNextLineHdrRecvBuf( + res_p, 12, res_p->headerLen, &labelend, &return_code_size); + + while (linetop > 0) + { + nextlinetop = NHTTPi_findNextLineHdrRecvBuf(res_p, linetop, + res_p->headerLen, &labelend, &return_code_size); + + if (labelend > 0) + { + labeltop = linetop; + + if (NHTTPi_compareTokenN_HdrRecvBuf( + res_p, labeltop, labelend, label_p, 0) + == 0) + { + if (labelend + 1 < res_p->headerLen) + { + valueend = NHTTPi_findNextLineHdrRecvBuf(res_p, + labelend + 1, res_p->headerLen, NULL, + &return_code_size); + if (valueend <= 0) + { + valueend = res_p->headerLen; + } + else + { + if (valueend < return_code_size) + { + return -1; + } + valueend -= return_code_size; + } + + valuetop = NHTTPi_skipSpaceHdrRecvBuf( + res_p, labelend + 1, valueend); + if (valuetop < 0) + { + valuetop = valueend; + } + + *pos_p = valuetop; + return valueend - valuetop; + } + else + { + return 0; + } + } + } + + linetop = nextlinetop; + } + + return -1; +} +s32 NHTTP_GetHeaderField(NHTTPResponseInfo* res_p, const char* label_p, + const char** value_p) +{ + s32 pos; + s32 length; + + if (res_p->isHeaderParse == 0) + { + return -1; + } + + if (res_p->foundHeader_p != NULL) + { + NHTTPi_free(res_p->foundHeader_p); + res_p->foundHeader_p = NULL; + } + + length = NHTTPi_getHeaderValue(res_p, label_p, &pos); + if (length >= 0) + { + res_p->foundHeader_p = NHTTPi_alloc(length + 1, 4); + if (res_p->foundHeader_p == NULL) + { + return -1; + } + res_p->foundHeader_p[length] = 0; + NHTTPi_loadFromHdrRecvBuf(res_p, res_p->foundHeader_p, pos, length); + *value_p = res_p->foundHeader_p; + return length; + } + + if (NHTTPi_strcmp("HTTPSTATUSCODE", label_p) == 0) + { + res_p->foundHeader_p = NHTTPi_alloc(4, 4); + if (res_p->foundHeader_p == NULL) + { + return -1; + } + res_p->foundHeader_p[3] = 0; + NHTTPi_loadFromHdrRecvBuf(res_p, res_p->foundHeader_p, 9, 3); + *value_p = res_p->foundHeader_p; + return 3; + } + + return -1; +} + +s32 NHTTP_GetHeaderAll(void* bgnEndInfo_p, NHTTPResponseInfo* res_p, + const char** header_p) +{ + if (res_p->isHeaderParse == 0 || res_p->headerLen == 0) + { + return -1; + } + + if (res_p->headerLen < NHTTP_HDRRECVBUF_INILEN) + { + *header_p = (const char*)res_p->hdrBufFirst; + } + else + { + if (res_p->allHeader_p == NULL) + { + res_p->allHeader_p = NHTTPi_alloc(res_p->headerLen, 4); + if (res_p->allHeader_p == NULL) + { + NHTTPi_SetError(bgnEndInfo_p, NHTTP_ERROR_ALLOC); + return -1; + } + NHTTPi_loadFromHdrRecvBuf(res_p, res_p->allHeader_p, 0, + res_p->headerLen); + } + *header_p = res_p->allHeader_p; + } + + return res_p->headerLen; +} + diff --git a/libs/RevoEX/src/nhttp/d_nhttp.c b/libs/RevoEX/src/nhttp/d_nhttp.c new file mode 100644 index 00000000..63620488 --- /dev/null +++ b/libs/RevoEX/src/nhttp/d_nhttp.c @@ -0,0 +1,673 @@ +#include + +#include + +#include +#include + + +struct NHTTPBgnEndInfo +{ + NCDIpConfig ipConfig; +}; + +typedef s32 (*NHTTPPostSend)(const char* label, char** value_p, + u32* length_p, s32 offset, void* userParam); + +void* NHTTPi_GetSystemInfoP(void); +NHTTPBgnEndInfo* NHTTPi_GetBgnEndInfoP(void* systemInfo); +void* NHTTPi_GetMutexInfoP(void* systemInfo); +BOOL NHTTPi_Startup(void* systemInfo, NHTTPAlloc alloc, NHTTPFree free, + u32 priority); +void NHTTPi_CleanupAsync(void* systemInfo, NHTTPCleanupCallback callback); +s32 NHTTPi_GetError(NHTTPBgnEndInfo* info); +void* NHTTPi_alloc(u32 size, int align); +void NHTTPi_free(void* buffer); +s32 NHTTPi_strlen(const char* string); +void* NHTTPi_memcpy(void* destination, const void* source, u32 size); +void* NHTTPi_memclr(void* destination, u32 size); +s32 NHTTPi_Base64Encode(char* destination, const char* source); +NHTTPConnectionInfo* NHTTPCreateConnection(const char* url, s32 method, + void* buffer, u32 bufferSize, NHTTPConnectionCallback callback, + void* userParam); +s32 NHTTPStartConnection(NHTTPConnectionInfo* connection); +s32 NHTTPGetBodyBuffer(NHTTPConnectionInfo* connection, void** buffer, + u32* bufferSize); +void* NHTTPGetUserParam(NHTTPConnectionInfo* connection); +s32 NHTTPGetConnectionError(NHTTPConnectionInfo* connection); +NHTTPConnectionInfo* NHTTPi_GetConnection(void* mutexInfo, + NHTTPConnectionInfo* connection); +NHTTPRequestInfo* NHTTPi_Connection2Request(void* mutexInfo, + NHTTPConnectionInfo* connection); +NHTTPResponseInfo* NHTTPi_Connection2Response(void* mutexInfo, + NHTTPConnectionInfo* connection); +NHTTPConnectionInfo* NHTTPi_Request2Connection(void* mutexInfo, + NHTTPRequestInfo* request); +NHTTPConnectionInfo* NHTTPi_Response2Connection(void* mutexInfo, + NHTTPResponseInfo* response); +NHTTPRequestInfo* NHTTPi_GetRequest(void* mutexInfo, + NHTTPRequestInfo* request); +NHTTPResponseInfo* NHTTPi_GetResponse(void* mutexInfo, + NHTTPResponseInfo* response); +s32 NHTTPi_OmitConnectionList(void* mutexInfo, + NHTTPConnectionInfo* connection); +u32 NHTTPi_GetVirtualContentLength(NHTTPConnectionInfo* connection); +BOOL NHTTP_AddHeaderField(NHTTPRequestInfo* request, NHTTPBgnEndInfo* info, + const char* name, const char* value); +BOOL NHTTP_AddPostDataAscii(NHTTPRequestInfo* request, NHTTPBgnEndInfo* info, + const char* name, const char* value); +BOOL NHTTP_CancelRequestAsync(void* systemInfo, s32 requestId); +void NHTTP_DestroyRequest(void* systemInfo, NHTTPRequestInfo* request); +void NHTTP_DestroyResponse(void* mutexInfo, NHTTPResponseInfo* response); + +static s32 nhttpRegistered = 0; + +const char* __NHTTPVersion = "<< RVL_SDK - NHTTP \trelease build: Jun 27 2007 16:27:59 (0x4199_60726) >>"; +char __NHTTPUnofficialVersion[] = "<< RVL_SDK - NHTTP \trelease build: Jun 27 2007 16:27:59 (0x4199_60726) UNOFFICIAL >>"; + +s32 NHTTPStartup(NHTTPAlloc alloc, NHTTPFree free, u32 priority) +{ + void* system = NHTTPi_GetSystemInfoP(); + if (nhttpRegistered == 0) + { + OSRegisterVersion(__NHTTPVersion); + nhttpRegistered = TRUE; + } + return NHTTPi_Startup(system, alloc, free, priority) ? 0 : -1; +} + +void NHTTPCleanupAsync(NHTTPCleanupCallback callback) +{ + NHTTPi_CleanupAsync(NHTTPi_GetSystemInfoP(), callback); +} + +s32 NHTTPGetError(void) +{ + return NHTTPi_GetError(NHTTPi_GetSystemInfoP()); +} + +static s32 NHTTPi_PostSendCallbackWrap(void* mutexInfo_p, + NHTTPConnectionInfo* connection_p, void* arg_p) +{ + if (connection_p != NULL) + { + NHTTPRequestInfo* req_p = + NHTTPi_Connection2Request(mutexInfo_p, connection_p); + if (req_p != NULL) + { + NHTTPPostSend postSend = (NHTTPPostSend)req_p->_unk250; + if (postSend != NULL) + { + NHTTPPostSendArg* postSendArg_p = arg_p; + return postSend(postSendArg_p->label, + &postSendArg_p->buf, &postSendArg_p->size, + postSendArg_p->offset, + NHTTPGetUserParam(connection_p)); + } + } + } + return -1; +} + +static s32 NHTTPi_BufFullCallbackWrap(void* mutexInfo_p, + NHTTPConnectionInfo* connection_p, void* arg_p) +{ + if (connection_p != NULL) + { + NHTTPResponseInfo* res_p = + NHTTPi_Connection2Response(mutexInfo_p, connection_p); + if (res_p != NULL) + { + NHTTPResponseCallback bufFull = res_p->bufFull; + if (bufFull != NULL) + { + NHTTPBodyBufArg* bodyBufArg_p = arg_p; + char* oldBuf_p = bodyBufArg_p->buf; + u32 contentLength = + NHTTPi_GetVirtualContentLength(connection_p); + bodyBufArg_p->buf = bufFull((void**)&oldBuf_p, + &bodyBufArg_p->size, contentLength, NHTTPi_alloc, + NHTTPi_free, NHTTPGetUserParam(connection_p)); + if (bodyBufArg_p->buf != NULL && oldBuf_p != NULL) + { + bodyBufArg_p->offset = 0; + } + } + } + } + return 0; +} + +static s32 NHTTPi_CompleteCallbackWrap(void* mutexInfo_p, + NHTTPConnectionInfo* connection_p) +{ + if (connection_p != NULL) + { + NHTTPReqCallback reqCallback = connection_p->requestCallback; + if (reqCallback != NULL) + { + NHTTPResponseInfo* res_p = + NHTTPi_Connection2Response(mutexInfo_p, connection_p); + if (res_p != NULL) + { + NHTTPErr result = NHTTPGetConnectionError(connection_p); + reqCallback( + result, res_p, NHTTPGetUserParam(connection_p)); + } + } + } + return 0; +} + +s32 NHTTPi_TemplateConnectionCallback(NHTTPConnectionInfo* handle, + NHTTPConnectionEvent event, void* arg_p) +{ + void* sysInfo_p = NHTTPi_GetSystemInfoP(); + void* mutexInfo_p = NHTTPi_GetMutexInfoP(sysInfo_p); + NHTTPConnectionInfo* connection_p = + NHTTPi_GetConnection(mutexInfo_p, handle); + s32 ret = 0; + + switch (event) + { + case NHTTP_EVENT_POST_SEND: + ret = NHTTPi_PostSendCallbackWrap( + mutexInfo_p, connection_p, arg_p); + break; + case NHTTP_EVENT_BODY_RECV_FULL: + ret = NHTTPi_BufFullCallbackWrap( + mutexInfo_p, connection_p, arg_p); + break; + case NHTTP_EVENT_BODY_RECV_DONE: + break; + case NHTTP_EVENT_COMPLETE: + ret = NHTTPi_CompleteCallbackWrap(mutexInfo_p, connection_p); + break; + default: + break; + } + return ret; +} + +NHTTPRequest* NHTTPCreateRequest(const char* url, NHTTPReqMethod method, + void* buffer, u32 bufferSize, NHTTPReqCallback callback, void* userParam) +{ + return NHTTPCreateRequestEx(url, method, buffer, bufferSize, callback, userParam, NULL, NULL); +} + +NHTTPRequest* NHTTPCreateRequestEx(const char* url, NHTTPReqMethod method, + void* buffer, u32 bufferSize, NHTTPReqCallback callback, void* userParam, + NHTTPResponseCallback responseCallback, NHTTPResponseCleanup cleanup) +{ + void* systemInfo; + void* mutexInfo; + NHTTPConnectionInfo* connection; + NHTTPRequestInfo* request; + + connection = NHTTPCreateConnection(url, method, buffer, bufferSize, NHTTPi_TemplateConnectionCallback, userParam); + request = NULL; + if (connection != NULL) + { + systemInfo = NHTTPi_GetSystemInfoP(); + mutexInfo = NHTTPi_GetMutexInfoP(systemInfo); + connection = NHTTPi_GetConnection(mutexInfo, connection); + request = NHTTPi_Connection2Request(mutexInfo, connection); + if (request != NULL) + { + if (request->response != NULL) + { + connection->requestCallback = callback; + request->response->bufFull = responseCallback; + request->response->freeBuf = cleanup; + return request; + } + else + { + NHTTP_DestroyRequest(systemInfo, request); + NHTTPi_OmitConnectionList(mutexInfo, connection); + NHTTPi_free(connection); + request = NULL; + } + } + } + return request; +} + +s32 NHTTPDeleteRequest(NHTTPRequest* request) +{ + NHTTPConnectionInfo* connection = NHTTPi_Request2Connection( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), request); + if (connection != NULL) + { + NHTTPDeleteConnection(connection); + } +} + +s32 NHTTPAddHeaderField(NHTTPRequest* handle, const char* name, + const char* value) +{ + void* systemInfo; + NHTTPRequestInfo* request; + void* bgnEndInfo; + + systemInfo = NHTTPi_GetSystemInfoP(); + request = NHTTPi_GetRequest(NHTTPi_GetMutexInfoP(systemInfo), handle); + bgnEndInfo = NHTTPi_GetBgnEndInfoP(systemInfo); + if (request == NULL) + { + return -1; + } + if (request->state != 0) + { + static char name[] = "NHTTPAddHeaderField"; + printf("%s can be called before NHTTPStartConnection()\n", name); + return -1; + } + return NHTTP_AddHeaderField(request, bgnEndInfo, name, value) ? 0 : -1; +} + +s32 NHTTPAddPostDataAscii(NHTTPRequest* handle, const char* name, + const char* value) +{ + NHTTPRequestInfo* request; + void* bgnEndInfo; + + bgnEndInfo = NHTTPi_GetBgnEndInfoP(NHTTPi_GetSystemInfoP()); + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (request == NULL) + { + return -1; + } + if (request->state != 0) + { + printf("%s can be called before NHTTPStartConnection()\n", + "NHTTPAddPostDataAscii"); + return -1; + } + return NHTTP_AddPostDataAscii(request, bgnEndInfo, name, value) + ? 0 + : -1; +} + +s32 NHTTPAddPostDataBinary(NHTTPRequest* handle, const char* name, + const char* value) +{ + NHTTPRequestInfo* request; + void* bgnEndInfo; + + bgnEndInfo = NHTTPi_GetBgnEndInfoP(NHTTPi_GetSystemInfoP()); + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (request == NULL) + { + return -1; + } + if (request->state != 0) + { + printf("%s can be called before NHTTPStartConnection()\n", + "NHTTPAddPostDataBinary"); + return -1; + } + return NHTTP_AddPostDataAscii(request, bgnEndInfo, name, value) + ? 0 + : -1; +} + +s32 NHTTPAddPostDataRaw(NHTTPRequest* handle, const void* data, + u32 size, const char* label) +{ + NHTTPRequestInfo* request; + + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (request == NULL) + { + return -1; + } + if (request->state != 0) + { + printf("%s can be called before NHTTPStartConnection()\n", + "NHTTPAddPostDataRaw"); + return -1; + } + if (request->postData != NULL) + { + printf("already called NHTTPAddPostDataAscii or NHTTPAddPostDataBinary (exclusive fucntion)\n"); + return -1; + } + request->isRawData = TRUE; + request->postBuffer = data; + request->postBufferSize = size; + return 0; +} + +s32 NHTTPSetPostDataEncoding(NHTTPRequest* handle, u32 encodingType) +{ + NHTTPRequestInfo* request; + + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (request == NULL) + { + return -1; + } + if (request->state != 0) + { + printf("%s can be called before NHTTPStartConnection()\n", + "NHTTPSetPostDataEncoding"); + return -1; + } + request->encodingType = encodingType; + return 0; +} + +NHTTPConnection NHTTPSendRequestAsync(NHTTPRequest* request) +{ + NHTTPConnectionInfo* connection; + s32 result; + + connection = NHTTPi_Request2Connection( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), request); + if (connection != NULL && NHTTPStartConnection(connection) == 0) + { + result = connection->requestId; + } + else + { + result = -1; + } + return result; +} + +s32 NHTTPCancelRequestAsync(NHTTPConnection connection) +{ + return NHTTP_CancelRequestAsync(NHTTPi_GetSystemInfoP(), connection) + ? 0 + : -1; +} + +s32 NHTTPGetProgress(u32* complete, u32* total) +{ + s32 result; + result = NHTTP_GetProgress(NHTTPi_GetSystemInfoP(), complete, total) + == 0; + return -result; +} + +void NHTTPDestroyResponse(NHTTPResponse* handle) +{ + void* mutexInfo; + NHTTPConnectionInfo* connection; + + mutexInfo = NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()); + connection = NHTTPi_Response2Connection(mutexInfo, handle); + if (connection != NULL) + { + if (NHTTPi_Connection2Response(mutexInfo, connection) != NULL) + { + NHTTP_DestroyResponse(mutexInfo, connection->response); + } + NHTTPi_OmitConnectionList(mutexInfo, connection); + NHTTPi_free(connection); + } +} + +s32 NHTTPGetHeaderField(NHTTPResponse* response, const char* name, + const char** value) +{ + void* mutexInfo; + NHTTPResponseInfo* res; + + mutexInfo = NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()); + res = NHTTPi_GetResponse(mutexInfo, response); + if (res == NULL) + { + return -1; + } + return NHTTP_GetHeaderField(res, name, value); +} + +s32 NHTTPGetHeaderAll(NHTTPResponse* response, const char** header) +{ + void* systemInfo; + NHTTPBgnEndInfo* bgnEndInfo; + NHTTPResponseInfo* res; + + systemInfo = NHTTPi_GetSystemInfoP(); + bgnEndInfo = NHTTPi_GetBgnEndInfoP(systemInfo); + res = NHTTPi_GetResponse(NHTTPi_GetMutexInfoP(systemInfo), response); + if (res == NULL) + { + return -1; + } + return NHTTP_GetHeaderAll(bgnEndInfo, res, header); +} + +s32 NHTTPGetBodyAll(NHTTPResponse* response, void** buffer) +{ + NHTTPConnectionInfo* connection; + u32 bufferSize; + s32 result; + + connection = NHTTPi_Response2Connection( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), response); + if (connection != NULL) + { + bufferSize = 0; + result = NHTTPGetBodyBuffer(connection, buffer, &bufferSize); + } + else + { + result = -1; + } + return result; +} + +s32 NHTTPGetResultCode(NHTTPResponse* handle) +{ + NHTTPResponseInfo* response; + response = NHTTPi_GetResponse( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (response == NULL) + return -1; + if (response->isHeaderParse != FALSE) + return response->httpStatus; + return -1; +} + +s32 NHTTPSetVerifyOption(NHTTPRequest* handle, u32 option) +{ + NHTTPRequestInfo* request; + s32 result; + + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (request == NULL) + { + result = -1; + } + else + { + request->verifyOption = option; + result = 0; + } + return result; +} + +s32 NHTTPSetProxy(NHTTPRequest* handle, const char* proxy_p, u16 port, + const char* username_p, const char* password_p) +{ + void* sysInfo_p = NHTTPi_GetSystemInfoP(); + void* mutexInfo_p = NHTTPi_GetMutexInfoP(sysInfo_p); + NHTTPRequestInfo* req_p = NHTTPi_GetRequest(mutexInfo_p, handle); + s32 proxyLen = 0; + + if (req_p == NULL || proxy_p == NULL) + { + return -1; + } + + proxyLen = NHTTPi_strlen(proxy_p); + if (proxyLen > 0x100) + { + printf("proxy-address exceeded 256 characters\n"); + return -1; + } + NHTTPi_memcpy(req_p->proxyServer, proxy_p, proxyLen); + req_p->proxyPort = port; + + if (username_p != NULL && password_p != NULL) + { + s32 username_len; + s32 password_len; + char tmpbuf[0x41]; + + username_len = NHTTPi_strlen(username_p); + password_len = NHTTPi_strlen(password_p); + if (username_len > 0x20) + { + printf("username exceeded 32 characters\n"); + return -1; + } + if (password_len > 0x20) + { + printf("password exceeded 32 characters\n"); + return -1; + } + NHTTPi_memclr(tmpbuf, sizeof(tmpbuf)); + NHTTPi_memcpy(tmpbuf, username_p, username_len); + NHTTPi_memcpy(&(tmpbuf[username_len]), ":", 1); + NHTTPi_memcpy( + &(tmpbuf[(username_len + 1)]), password_p, password_len); + req_p->proxyAuthorizationLength = + NHTTPi_Base64Encode(req_p->proxyAuthorization, tmpbuf); + } + + req_p->proxyEnabled = TRUE; + return 0; +} + +s32 NHTTPSetProxyDefault(NHTTPRequest* handle) +{ + NCDProxyServerProfile* proxy; + void* systemInfo; + NHTTPBgnEndInfo* bgnEndInfo; + NHTTPRequestInfo* request; + const char* username; + const char* password; + s32 result; + + systemInfo = NHTTPi_GetSystemInfoP(); + request = NHTTPi_GetRequest(NHTTPi_GetMutexInfoP(systemInfo), handle); + bgnEndInfo = NHTTPi_GetBgnEndInfoP(systemInfo); + if (request->secure != FALSE) + { + proxy = &bgnEndInfo->ipConfig.proxy.ssl; + } + else + { + proxy = &bgnEndInfo->ipConfig.proxy.http; + } + + if (proxy->mode == 1) + { + username = NULL; + password = NULL; + if (proxy->authType == 1) + { + username = proxy->username; + password = proxy->password; + } + OSReport("Using proxy server %s:%d (%s/%s).\n", proxy->server, proxy->port, username != NULL ? username : "[no-auth]", password != NULL ? password : "[no-auth]"); + result = NHTTPSetProxy(request, proxy->server, proxy->port, username, password); + if (result < 0) + { + OSReport("NHTTPSetProxy failed.(%d)\n", result); + OSPanic("d_nhttp.c", 0x37d, "NHTTPSetProxy"); + } + else + { + return 0; + } + } + else + { + printf("NCD proxy-settings are disabled\n"); + } + return -1; +} + +s32 NHTTPSetRootCADefault(NHTTPRequest* handle) +{ + s32 result; + NHTTPRequestInfo* request; + + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + + if (request == NULL) + { + result = -1; + } + else + { + request->builtinRootCA = FALSE; + request->rootCAData = NULL; + request->rootCASize = 0; + result = 0; + } + return result; +} + +s32 NHTTPSetClientCertDefault(NHTTPRequest* handle) +{ + s32 result; + NHTTPRequestInfo* request; + + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + + if (request == NULL) + { + result = -1; + } + else + { + request->builtinClientCert = FALSE; + request->clientCertDefault = TRUE; + request->clientCertData = NULL; + request->clientCertSize = 0; + request->privateKeyData = NULL; + request->privateKeySize = 0; + result = 0; + } + return result; +} + +s32 NHTTPDisableVerifyOptionForDebug(NHTTPRequest* handle, u32 option) +{ + NHTTPRequestInfo* request; + + request = NHTTPi_GetRequest( + NHTTPi_GetMutexInfoP(NHTTPi_GetSystemInfoP()), handle); + if (request == NULL) + { + return -1; + } + if (request->state != 0) + { + printf("%s can be called before NHTTPStartConnection()\n", + "NHTTPDisableVerifyOptionForDebug"); + return -1; + } + { + static s32 debugSet; + if (debugSet == 0) + { + debugSet = 1; + } + } + request->verifyOption &= ~option; + return 0; +} diff --git a/libs/RevoEX/src/nhttp/d_nhttp_common.c b/libs/RevoEX/src/nhttp/d_nhttp_common.c new file mode 100644 index 00000000..ad97c610 --- /dev/null +++ b/libs/RevoEX/src/nhttp/d_nhttp_common.c @@ -0,0 +1,421 @@ +#include + +#include + +typedef struct NHTTPSysInfo +{ + u8 bgnEndInfo[0x800]; + NHTTPListInfo listInfo; + NHTTPReqInfo reqInfo; + u8 mutexInfo[0x34]; + u8 threadInfo[0x460]; +} NHTTPSysInfo; + +typedef struct NHTTPStaticResource +{ + BOOL initialized; + OSMutex mutex; + OSCond cond; + u32 _unk24; + u8 _unk28[0x18]; +} NHTTPStaticResource; + +static NHTTPSysInfo sysInfo; +static NHTTPSysInfo* sysInfo_p; +static NHTTPConnectionInfo* sConnectionList; +static NHTTPStaticResource NHTTPiStaticResource; + +void NHTTPi_lockReqList(void* mutexInfo); +void NHTTPi_unlockReqList(void* mutexInfo); +void NHTTPi_InitBgnEndInfo(NHTTPBgnEndInfo* info); +void NHTTPi_InitListInfo(NHTTPListInfo* info); +void NHTTPi_InitRequestInfo(NHTTPReqInfo* info); +void NHTTPi_InitMutexInfo(void* info); +void NHTTPi_InitThreadInfo(void* info); +NHTTPBgnEndInfo* NHTTPi_GetBgnEndInfoP(NHTTPSysInfo* sysInfo_p); +NHTTPListInfo* NHTTPi_GetListInfoP(NHTTPSysInfo* sysInfo_p); +NHTTPReqInfo* NHTTPi_GetReqInfoP(NHTTPSysInfo* sysInfo_p); +void* NHTTPi_GetThreadInfoP(NHTTPSysInfo* sysInfo_p); +void* NHTTPi_GetMutexInfoP(NHTTPSysInfo* sysInfo_p); + +void NHTTPi_WaitForCompletion(NHTTPConnectionInfo* connection) +{ + NHTTPStaticResource* res_p = &NHTTPiStaticResource; + + if (!res_p->initialized) + { + OSInitMutex(&res_p->mutex); + OSInitCond(&res_p->cond); + res_p->initialized = TRUE; + } + OSLockMutex(&res_p->mutex); + while (connection->_unkC != 0) + { + OSWaitCond(&res_p->cond, &res_p->mutex); + } + OSUnlockMutex(&res_p->mutex); +} + +void NHTTPi_NotifyCompletion(NHTTPConnectionInfo* connection) +{ + NHTTPStaticResource* res_p = &NHTTPiStaticResource; + + if (!res_p->initialized) + { + OSInitMutex(&res_p->mutex); + OSInitCond(&res_p->cond); + res_p->initialized = TRUE; + } + OSLockMutex(&res_p->mutex); + connection->_unkC = 0; + OSSignalCond(&res_p->cond); + OSUnlockMutex(&res_p->mutex); +} + +NHTTPConnectionInfo* NHTTPi_ControlConnectionList(void* mutexInfo, + void* handle, u32 mode) +{ + NHTTPConnectionInfo* connection; + NHTTPConnectionInfo* result; + NHTTPConnectionInfo** current; + + result = NULL; + NHTTPi_lockReqList(mutexInfo); + if (mode == 3) + { + connection = handle; + connection->next = sConnectionList; + sConnectionList = connection; + result = connection; + } + else + { + current = &sConnectionList; + while (*current != NULL) + { + connection = *current; + switch (mode) + { + case 0: + if (connection == handle) + { + result = connection; + } + break; + case 1: + if (connection->request == handle) + { + result = connection; + } + break; + case 2: + if (connection->response == handle) + { + result = connection; + } + break; + case 4: + if (connection == handle) + { + *current = connection->next; + result = connection; + } + break; + } + + if (result != NULL) + { + break; + } + current = &(*current)->next; + } + } + NHTTPi_unlockReqList(mutexInfo); + return result; +} + +s32 NHTTPi_CommitConnectionList(void* mutexInfo_p, + NHTTPConnectionInfo* connection_p) +{ + return ((NHTTPi_ControlConnectionList(mutexInfo_p, connection_p, 3) + != NULL) + ? 0 + : -1); +} + +s32 NHTTPi_OmitConnectionList(void* mutexInfo_p, + NHTTPConnectionInfo* connection_p) +{ + return ((NHTTPi_ControlConnectionList(mutexInfo_p, connection_p, 4) + != NULL) + ? 0 + : -1); +} + +NHTTPRequestInfo* NHTTPi_Connection2Request(void* mutexInfo, + NHTTPConnectionInfo* connection) +{ + connection = NHTTPi_ControlConnectionList(mutexInfo, connection, 0); + return connection != NULL ? connection->request : NULL; +} + +NHTTPResponseInfo* NHTTPi_Connection2Response(void* mutexInfo, + NHTTPConnectionInfo* connection) +{ + connection = NHTTPi_ControlConnectionList(mutexInfo, connection, 0); + return connection != NULL ? connection->response : NULL; +} + +NHTTPConnectionInfo* NHTTPi_Request2Connection(void* mutexInfo, + NHTTPRequestInfo* request) +{ + return NHTTPi_ControlConnectionList( + mutexInfo, request, 1); +} + +NHTTPConnectionInfo* NHTTPi_Response2Connection(void* mutexInfo, + NHTTPResponseInfo* response) +{ + return NHTTPi_ControlConnectionList( + mutexInfo, response, 2); +} + +NHTTPConnectionInfo* NHTTPi_GetConnection(void* mutexInfo, + NHTTPConnectionInfo* connection) +{ + return NHTTPi_ControlConnectionList(mutexInfo, connection, 0); +} + +NHTTPRequestInfo* NHTTPi_GetRequest(void* mutexInfo, + NHTTPRequestInfo* request) +{ + NHTTPConnectionInfo* connection; + + connection = NHTTPi_ControlConnectionList( + mutexInfo, request, 0); + return connection != NULL ? connection->request : request; +} + +NHTTPResponseInfo* NHTTPi_GetResponse(void* mutexInfo, + NHTTPResponseInfo* response) +{ + NHTTPConnectionInfo* connection; + + connection = NHTTPi_ControlConnectionList( + mutexInfo, response, 0); + return connection != NULL ? connection->response : response; +} + +s32 NHTTPi_GetConnectionListLength(void) +{ + NHTTPConnectionInfo* connection; + s32 length; + + connection = sConnectionList; + length = 0; + while (connection != NULL) + { + connection = connection->next; + length++; + } + return length; +} + +s32 NHTTPi_PostSendCallback(void* mutexInfo, + NHTTPConnectionInfo* connection, void* value, u32 arg) +{ + u32 callbackArg4; + u32 callbackArg8; + s32 result; + NHTTPResponseInfo* response; + NHTTPConnectionCallbackParam param; + + result = -1; + if (NHTTPi_GetConnection(mutexInfo, connection) != NULL) + { + response = NHTTPi_Connection2Response(mutexInfo, connection); + if (response != NULL && connection->callback != NULL) + { + param.value = value; + param._unk4 = connection->_unk24; + param._unk8 = connection->_unk28; + param._unkC = arg; + result = connection->callback(connection, 1, ¶m); + callbackArg8 = param._unk8; + callbackArg4 = param._unk4; + + connection = NHTTPi_GetConnection(mutexInfo, connection); + if (connection != NULL) + { + connection->_unk24 = callbackArg4; + connection->_unk28 = callbackArg8; + } + } + } + return result; +} + +void NHTTPi_BufferFullCallback(void* mutexInfo, + NHTTPConnectionInfo* connection) +{ + void* buffer; + u32 bufferSize; + s32 responseResult; + NHTTPResponseInfo* response; + NHTTPConnectionCallbackParam param; + + if (NHTTPi_GetConnection(mutexInfo, connection) != NULL) + { + response = NHTTPi_Connection2Response(mutexInfo, connection); + if (response != NULL && connection->callback != NULL) + { + param.value = response->recvBuf_p; + param._unk4 = response->recvBufLen; + param._unk8 = response->bodyLen; + connection->callback(connection, 2, ¶m); + responseResult = param._unk8; + bufferSize = param._unk4; + buffer = param.value; + + connection = NHTTPi_GetConnection(mutexInfo, connection); + if (connection != NULL) + { + response = NHTTPi_Connection2Response(mutexInfo, connection); + if (response != NULL) + { + response->recvBuf_p = buffer; + response->recvBufLen = bufferSize; + response->bodyLen = responseResult; + } + } + } + } +} + +void NHTTPi_ReceivedCallback(void* mutexInfo, + NHTTPConnectionInfo* connection) +{ + void* buffer; + u32 bufferSize; + s32 responseResult; + NHTTPResponseInfo* response; + NHTTPConnectionCallbackParam param; + + if (NHTTPi_GetConnection(mutexInfo, connection) != NULL) + { + response = NHTTPi_Connection2Response(mutexInfo, connection); + if (response != NULL && connection->callback != NULL) + { + param.value = response->recvBuf_p; + param._unk4 = response->recvBufLen; + param._unk8 = response->bodyLen; + connection->callback(connection, 3, ¶m); + responseResult = param._unk8; + bufferSize = param._unk4; + buffer = param.value; + + connection = NHTTPi_GetConnection(mutexInfo, connection); + if (connection != NULL) + { + response = NHTTPi_Connection2Response(mutexInfo, connection); + if (response != NULL) + { + response->recvBuf_p = buffer; + response->recvBufLen = bufferSize; + response->bodyLen = responseResult; + } + } + } + } +} + +void NHTTPi_CompleteCallback(void* mutexInfo, + NHTTPConnectionInfo* connection) +{ + if (NHTTPi_GetConnection(mutexInfo, connection) != NULL + && connection->callback != NULL) + { + connection->callback(connection, 4, NULL); + } +} + +static void NHTTPi_InitSystemInfo(NHTTPSysInfo* sysInfo_p) +{ + NHTTPi_InitBgnEndInfo(NHTTPi_GetBgnEndInfoP(sysInfo_p)); + NHTTPi_InitListInfo(NHTTPi_GetListInfoP(sysInfo_p)); + NHTTPi_InitRequestInfo(NHTTPi_GetReqInfoP(sysInfo_p)); + NHTTPi_InitMutexInfo(NHTTPi_GetMutexInfoP(sysInfo_p)); + NHTTPi_InitThreadInfo(NHTTPi_GetThreadInfoP(sysInfo_p)); +} + +NHTTPSysInfo* NHTTPi_GetSystemInfoP(void) +{ + if (sysInfo_p == NULL) + { + sysInfo_p = &sysInfo; + NHTTPi_InitSystemInfo(sysInfo_p); + } + return sysInfo_p; +} + +NHTTPBgnEndInfo* NHTTPi_GetBgnEndInfoP(NHTTPSysInfo* sysInfo_p) +{ + return (NHTTPBgnEndInfo*)sysInfo_p->bgnEndInfo; +} + +NHTTPListInfo* NHTTPi_GetListInfoP(NHTTPSysInfo* sysInfo_p) +{ + return &sysInfo_p->listInfo; +} + +NHTTPReqInfo* NHTTPi_GetReqInfoP(NHTTPSysInfo* sysInfo_p) +{ + return &sysInfo_p->reqInfo; +} + +void* NHTTPi_GetThreadInfoP(NHTTPSysInfo* sysInfo_p) +{ + return sysInfo_p->threadInfo; +} + +void* NHTTPi_GetMutexInfoP(NHTTPSysInfo* sysInfo_p) +{ + return sysInfo_p->mutexInfo; +} + +void NHTTPi_SetVirtualContentLength(NHTTPConnectionInfo* connection, + u32 length) +{ + if (connection != NULL) + { + connection->virtualContentLength = length; + } +} + +u32 NHTTPi_GetVirtualContentLength(NHTTPConnectionInfo* connection) +{ + return connection != NULL ? connection->virtualContentLength : 0; +} + +void NHTTPi_InitConnectionList(void) +{ + NHTTPConnectionInfo* connection; + s32 count; + + connection = sConnectionList; + if (connection != NULL) + { + count = 0; + while (connection != NULL) + { + connection = connection->next; + count++; + } + if (count != 0) + { + printf("*warning: %d connections rests! Please free connections.\n", + count); + } + } + sConnectionList = NULL; +}