static bool NetlibHttpGatewayStdPost(NetlibConnection *nlc, int& numPackets) { int np = 0, len = 0; char *buf; { mir_cslock lck(nlc->csHttpSequenceNums); NetlibHTTPProxyPacketQueue *p = nlc->pHttpProxyPacketQueue; while (p != NULL && np < nlc->nlhpi.combinePackets) { ++np; len += p->dataBufferLen; p = p->next;} buf = (char*)alloca(len); numPackets = np; int dlen = 0; p = nlc->pHttpProxyPacketQueue; while (np--) { memcpy(buf + dlen, p->dataBuffer, p->dataBufferLen); dlen += p->dataBufferLen; p = p->next; } } return NetlibHttpGatewaySend(nlc, reqNewPost, buf, len); }
static bool NetlibHttpGatewayOscarPost(NetlibConnection *nlc, const char *buf, int len, int flags) { NETLIBHTTPREQUEST *nlhrReply = NULL; NetlibConnection nlcSend = {0}; nlcSend.handleType = NLH_CONNECTION; nlcSend.nlu = nlc->nlu; nlcSend.nlhpi = nlc->nlhpi; nlcSend.s = nlc->s2; nlcSend.usingHttpGateway = nlc->usingHttpGateway; nlcSend.szProxyServer = nlc->szProxyServer; nlcSend.wProxyPort = nlc->wProxyPort; nlcSend.proxyType = nlc->proxyType; if (!NetlibReconnect(&nlcSend)) return false; nlc->s2 = nlcSend.s; nlcSend.hOkToCloseEvent = CreateEvent(NULL, TRUE, TRUE, NULL); NetlibInitializeNestedCS(&nlcSend.ncsRecv); NetlibInitializeNestedCS(&nlcSend.ncsSend); bool res = NetlibHttpGatewaySend(&nlcSend, reqOldPost, buf, len); if (res) { NETLIBHTTPREQUEST *nlhrReply = NetlibHttpRecv(&nlcSend, flags | MSG_RAW | MSG_DUMPPROXY, MSG_RAW | MSG_DUMPPROXY); if (nlhrReply != NULL) { if (nlhrReply->resultCode != 200) { NetlibHttpSetLastErrorUsingHttpResult(nlhrReply->resultCode); res = false; } NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); } else res = false; } NetlibDeleteNestedCS(&nlcSend.ncsSend); NetlibDeleteNestedCS(&nlcSend.ncsRecv); CloseHandle(nlcSend.hOkToCloseEvent); nlc->s2 = nlcSend.s; mir_free((char*)nlcSend.nloc.szHost); EnterCriticalSection(&nlc->csHttpSequenceNums); nlc->nlhpi.firstPostSequence++; if (nlc->nlhpi.flags & NLHPIF_GETPOSTSAMESEQUENCE) nlc->nlhpi.firstGetSequence++; LeaveCriticalSection(&nlc->csHttpSequenceNums); return res; }
int NetlibInitHttpConnection(NetlibConnection *nlc, NetlibUser *nlu, NETLIBOPENCONNECTION *nloc) { NETLIBHTTPREQUEST *nlhrReply = NULL; nlc->nlhpi.firstGetSequence = 1; nlc->nlhpi.firstPostSequence = 1; if (nlu->user.szHttpGatewayHello != NULL) { nlc->usingHttpGateway = true; if (NetlibHttpGatewaySend(nlc, reqHelloGet, NULL, 0)) nlhrReply = NetlibHttpRecv(nlc, MSG_DUMPPROXY | MSG_RAW, MSG_DUMPPROXY | MSG_RAW); nlc->usingHttpGateway = false; if (nlhrReply == NULL) return 0; if (nlhrReply->resultCode != 200) { NetlibHttpSetLastErrorUsingHttpResult(nlhrReply->resultCode); NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); return 0; } } if (!nlu->user.pfnHttpGatewayInit(nlc, nloc, nlhrReply)) { NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); return 0; } NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); /* * Gena01 - Ok, we should be able to use just POST. Needed for Yahoo, NO GET requests */ if (nlc->nlhpi.szHttpPostUrl == NULL) { SetLastError(ERROR_BAD_FORMAT); return 0; } nlc->usingHttpGateway = true; //now properly connected if (nlu->user.pfnHttpGatewayBegin && !nlu->user.pfnHttpGatewayBegin(nlc, nloc)) return 0; return 1; }
static bool NetlibHttpGatewayOscarPost(NetlibConnection *nlc, const char *buf, int len, int flags) { NetlibConnection *nlcSend = new NetlibConnection(); nlcSend->nlu = nlc->nlu; nlcSend->nlhpi = nlc->nlhpi; nlcSend->s = nlc->s2; nlcSend->usingHttpGateway = nlc->usingHttpGateway; nlcSend->szProxyServer = nlc->szProxyServer; nlcSend->wProxyPort = nlc->wProxyPort; nlcSend->proxyType = nlc->proxyType; if (!NetlibReconnect(nlcSend)) { delete nlcSend; return false; } nlc->s2 = nlcSend->s; bool res = NetlibHttpGatewaySend(nlcSend, reqOldPost, buf, len); if (res) { NETLIBHTTPREQUEST *nlhrReply = NetlibHttpRecv(nlcSend, flags | MSG_RAW | MSG_DUMPPROXY, MSG_RAW | MSG_DUMPPROXY); if (nlhrReply != NULL) { if (nlhrReply->resultCode != 200) { NetlibHttpSetLastErrorUsingHttpResult(nlhrReply->resultCode); res = false; } NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); } else res = false; } nlc->s2 = nlcSend->s; delete nlcSend; mir_cslock lck(nlc->csHttpSequenceNums); nlc->nlhpi.firstPostSequence++; if (nlc->nlhpi.flags & NLHPIF_GETPOSTSAMESEQUENCE) nlc->nlhpi.firstGetSequence++; return res; }
int NetlibHttpGatewayRecv(NetlibConnection *nlc, char *buf, int len, int flags) { bool peek = (flags & MSG_PEEK) != 0; if (nlc->dataBufferLen != 0 && (!peek || nlc->dataBufferLen >= len)) { return HttpGatewayReadSetResult(nlc, buf, len, peek); } for (int retryCount = 0; retryCount < NETLIBHTTP_RETRYCOUNT;) { if (nlc->nlhpi.szHttpGetUrl == NULL && retryCount == 0) { if (nlc->pollingTimeout == 0) nlc->pollingTimeout = 30; /* We Need to sleep/wait for the data to send before we do receive */ for (int pollCount = nlc->pollingTimeout; pollCount--;) { if (nlc->pHttpProxyPacketQueue != NULL && GetTickCount() - nlc->lastPost > 1000) break; if (nlc->termRequested || (SleepEx(1000, TRUE) && Miranda_Terminated())) return SOCKET_ERROR; } nlc->lastPost = GetTickCount(); if (nlc->pHttpProxyPacketQueue == NULL && nlc->nlu->user.pfnHttpGatewayWrapSend != NULL) { if (nlc->nlu->user.pfnHttpGatewayWrapSend(nlc, (PBYTE)"", 0, MSG_NOHTTPGATEWAYWRAP, NetlibSend) == SOCKET_ERROR) return SOCKET_ERROR; } } int numPackets = 0; if (nlc->nlhpi.szHttpGetUrl) { if (!NetlibHttpGatewaySend(nlc, reqOldGet, NULL, 0)) { if (GetLastError() == ERROR_ACCESS_DENIED || nlc->termRequested) break; ++retryCount; continue; } } else { if (!NetlibHttpGatewayStdPost(nlc, numPackets)) { if (GetLastError() == ERROR_ACCESS_DENIED || nlc->termRequested) break; ++retryCount; continue; } } NETLIBHTTPREQUEST *nlhrReply = NetlibHttpRecv(nlc, flags | MSG_RAW | MSG_DUMPPROXY, MSG_RAW | MSG_DUMPPROXY); if (nlhrReply == NULL) return SOCKET_ERROR; if (nlc->nlu->user.pfnHttpGatewayUnwrapRecv && !(flags & MSG_NOHTTPGATEWAYWRAP)) { nlhrReply->pData = (char*)nlc->nlu->user.pfnHttpGatewayUnwrapRecv(nlhrReply, (PBYTE)nlhrReply->pData, nlhrReply->dataLength, &nlhrReply->dataLength, mir_realloc); } if (nlhrReply->resultCode >= 300) { int resultCode = nlhrReply->resultCode; NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); if (nlc->nlhpi.szHttpGetUrl && resultCode != 404) { NetlibLogf(nlc->nlu, "Error received from proxy, retrying"); continue; } else { NetlibLogf(nlc->nlu, "Error received from proxy, retry attempts exceeded (%u)", retryCount); SetLastError(ERROR_GEN_FAILURE); return SOCKET_ERROR; } } else { retryCount = 0; HttpGatewayRemovePacket(nlc, numPackets); } if (nlhrReply->dataLength) { if (peek) { int rbytes = nlc->dataBufferLen + nlhrReply->dataLength; nlc->dataBuffer = (PBYTE)mir_realloc(nlc->dataBuffer, rbytes); memcpy(nlc->dataBuffer + nlc->dataBufferLen, nlhrReply->pData, nlhrReply->dataLength); nlc->dataBufferLen = rbytes; NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); return HttpGatewayReadSetResult(nlc, buf, len, peek); } else { int bytes = min(len, nlhrReply->dataLength); int rbytes = nlhrReply->dataLength - bytes; memcpy(buf, nlhrReply->pData, bytes); nlc->dataBuffer = (PBYTE)mir_realloc(nlc->dataBuffer, rbytes); if (rbytes) memcpy(nlc->dataBuffer, nlhrReply->pData + bytes, rbytes); nlc->dataBufferLen = rbytes; NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); return bytes; } } else { if ((peek && nlc->dataBufferLen != 0) || nlhrReply->pData) { NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); return HttpGatewayReadSetResult(nlc, buf, len, peek); } } NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply); } SetLastError(ERROR_GEN_FAILURE); return SOCKET_ERROR; }