Advertisement
Guest User

Untitled

a guest
Nov 21st, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. map<HINTERNET, HttpRequest*> requestMap;
  2.  
  3. // ======================================================================================
  4. // ======================================================================================
  5.  
  6. void __stdcall httpRequestsCallback (HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus, LPVOID lpStatusInfo, DWORD dwStatusInfoLen);
  7.  
  8. // ======================================================================================
  9. // ======================================================================================
  10.  
  11. HttpRequest::HttpRequest (string domain, string URL) {
  12.     this->domain = domain;
  13.     this->URL = URL;
  14.     this->status = STATUS_OK;
  15.     this->openRequestState = 0;
  16.     this->sendRequestState = 0;
  17.     this->readResponseState = 0;
  18. }
  19.  
  20. // ======================================================================================
  21.  
  22. void HttpRequest::openInternet() {
  23.  
  24.     //
  25.     this->hInstance = InternetOpenA("asynchttp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
  26.     if (!this->hInstance) this->error();
  27.     requestMap[this->hInstance] = this;
  28.  
  29. }
  30.  
  31. // ======================================================================================
  32.  
  33. void HttpRequest::setupCallback() {
  34.  
  35.     //
  36.     if (InternetSetStatusCallback(this->hInstance, (INTERNET_STATUS_CALLBACK)&httpRequestsCallback) == INTERNET_INVALID_STATUS_CALLBACK) {
  37.         this->error();
  38.     }
  39.  
  40. }
  41.  
  42. // ======================================================================================
  43.  
  44. void HttpRequest::openConnection() {
  45.  
  46.     //
  47.     this->hConnect = InternetConnectA(this->hInstance, this->domain.c_str(), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
  48.  
  49.     //
  50.     if (hConnect != NULL) {
  51.         requestMap[this->hConnect] = this;
  52.     } else {
  53.         //
  54.         if (GetLastError() != ERROR_IO_PENDING) {
  55.             this->error();
  56.         }
  57.     }
  58.  
  59. }
  60.  
  61. // ======================================================================================
  62.  
  63. void HttpRequest::openRequest() {
  64.  
  65.     //
  66.     this->hRequest = HttpOpenRequestA(this->hConnect, "GET", this->URL.c_str(), NULL, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 2);
  67.    
  68.     //
  69.     if (this->hRequest != NULL) {
  70.         requestMap[this->hRequest] = this;
  71.     } else {
  72.         //
  73.         if (GetLastError() != ERROR_IO_PENDING) {
  74.             this->error();
  75.         }
  76.     }
  77.  
  78. }
  79.  
  80. // ======================================================================================
  81.  
  82. void HttpRequest::sendRequest() {
  83.  
  84.     //
  85.     if (!HttpSendRequestA(this->hRequest, NULL, 0, NULL, 0)) {
  86.         //
  87.         if (GetLastError() != ERROR_IO_PENDING) {
  88.             this->error();
  89.         }
  90.     }
  91.  
  92. }
  93.  
  94. // ======================================================================================
  95.  
  96. void HttpRequest::readResponse() {
  97.  
  98. int statusCode;
  99. DWORD statCharLen = 128;
  100. char statChar[128];
  101.  
  102. // Check the status code
  103. HttpQueryInfoA(this->hRequest, HTTP_QUERY_STATUS_CODE, &statChar, &statCharLen, NULL);
  104. statusCode = atoi(statChar);
  105. output(statChar);
  106.  
  107.     //
  108.     bool allDone = false;
  109.     char buffer[16368];
  110.     do {
  111.  
  112.         //
  113.         INTERNET_BUFFERSA InternetBuffer;
  114.         FillMemory(&InternetBuffer, sizeof(InternetBuffer), 0);
  115.         InternetBuffer.dwStructSize = sizeof(InternetBuffer);
  116.         InternetBuffer.lpvBuffer = buffer;
  117.         InternetBuffer.dwBufferLength = sizeof(buffer) - 1;
  118.  
  119.         //
  120.         if (!InternetReadFileExA(this->hRequest, &InternetBuffer, 0, 2 )) {
  121.             this->error();
  122.         }
  123.  
  124.         //
  125.         buffer[InternetBuffer.dwBufferLength] = 0;
  126.         this->result.append(buffer);
  127.  
  128.         output("What was received: ");
  129.         output(buffer);
  130.         output("dwBufferLength: " + intToStr(InternetBuffer.dwBufferLength));
  131.        
  132.         //
  133.         if (InternetBuffer.dwBufferLength == 0) {
  134.             allDone = true;
  135.         }
  136.  
  137.         // Look for the 'end data' character.
  138.         // (NOTE: This is an internal thing: I don't want to receive any data
  139.         //  that wuold be sent after the dollar character.)
  140.         string::size_type endCharPos = this->result.find('$');
  141.         if (endCharPos != string::npos) {
  142.             this->result = this->result.substr(0, endCharPos);
  143.             break;
  144.         }
  145.  
  146.  
  147.     } while (!allDone);
  148.  
  149.     //
  150.     this->status = STATUS_SUCCESS;
  151.  
  152. }
  153.  
  154. // ======================================================================================
  155.  
  156. void HttpRequest::send() {
  157.     //
  158.     this->openInternet();
  159.     this->setupCallback();
  160.     this->openConnection();
  161. }
  162.  
  163. // ======================================================================================
  164.  
  165. void HttpRequest::update() {
  166.  
  167.     //
  168.     if (this->openRequestState == 1) {
  169.         this->openRequest();
  170.         this->openRequestState = 2;
  171.     }
  172.     if (this->sendRequestState == 1) {
  173.         this->sendRequest();
  174.         this->sendRequestState = 2;
  175.     }
  176.     if (this->readResponseState == 1) {
  177.         this->readResponse();
  178.         this->readResponseState = 2;
  179.     }
  180.  
  181. }
  182.  
  183. // ======================================================================================
  184.  
  185. void HttpRequest::error() {
  186.     this->status = STATUS_ERROR;
  187. }
  188.  
  189. // ======================================================================================
  190. // ======================================================================================
  191.  
  192. void __stdcall httpRequestsCallback (HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus, LPVOID lpStatusInfo, DWORD dwStatusInfoLen) {
  193.  
  194.     //
  195.     HttpRequest* httpRequest = requestMap[hInternet];
  196.  
  197.     //
  198.     if (dwContext == 1)  {
  199.  
  200.         //
  201.         if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED) {
  202.             //
  203.             INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
  204.             httpRequest->hConnect = (HINTERNET)pRes->dwResult;
  205.             requestMap[httpRequest->hConnect] = httpRequest;
  206.             //
  207.             if (!httpRequest->openRequestState) httpRequest->openRequestState = 1;
  208.         }
  209.  
  210.     }
  211.  
  212.     //
  213.     if (dwContext == 2) {
  214.  
  215.         //
  216.         if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED) {
  217.             //
  218.             INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
  219.             httpRequest->hRequest = (HINTERNET)pRes->dwResult;
  220.             requestMap[httpRequest->hRequest] = httpRequest;
  221.             //
  222.             if (!httpRequest->sendRequestState) httpRequest->sendRequestState = 1;
  223.         }
  224.  
  225.         //
  226.         if (dwInternetStatus == INTERNET_STATUS_REQUEST_SENT) {
  227.             // In case we would be interested.
  228.             DWORD *lpBytesSent = (DWORD*)lpStatusInfo;
  229.         }
  230.        
  231.         //
  232.         if (dwInternetStatus == INTERNET_STATUS_REQUEST_COMPLETE) {
  233.             //
  234.             INTERNET_ASYNC_RESULT *pAsyncRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
  235.             if (!httpRequest->readResponseState) httpRequest->readResponseState = 1;
  236.         }
  237.  
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement