Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.8 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <memory.h>
  5. #include <wininet.h>
  6. #include "unicode.h"
  7. BYTE * HTTPGet(const WCHAR * wszURL, DWORD * pcbReceiveBuff) {
  8. HINTERNET hIOpen = NULL;
  9. HINTERNET hIConnect = NULL;
  10. HINTERNET hIHttp = NULL;
  11. BYTE * pbRecBuf = NULL;
  12. char * szPartURL = NULL;
  13. char szBuff[1024];
  14. char szLong[16];
  15. char szDomanName[_MAX_PATH];
  16. char szPort[12];
  17. char * pch;
  18. DWORD cch;
  19. DWORD dwService = INTERNET_SERVICE_HTTP;
  20. char * pchT;
  21. DWORD cbBuff, cbBuffRead, cbBuffT;
  22. DWORD dwPort = INTERNET_INVALID_PORT_NUMBER;
  23. char * szURL = NULL;
  24. assert(wszURL != NULL);
  25. assert(pcbReceiveBuff != NULL);
  26. *pcbReceiveBuff = 0;
  27. // figure out the protocol
  28. if( !MkMBStr(NULL, 0, wszURL, &szURL)) {
  29. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  30. goto ErrorReturn;
  31. }
  32. //
  33. // DSIE: Fix bug 112117
  34. //
  35. if (NULL == szURL) {
  36. return NULL;
  37. }
  38. cch = strlen(szURL);
  39. if(cch >= 7 && _strnicmp(szURL, "http://", 7) == 0) {
  40. dwService = INTERNET_SERVICE_HTTP;
  41. pch = (char *) &szURL[7];
  42. } else if(cch >= 6 && _strnicmp(szURL, "ftp://", 6) == 0) {
  43. dwService = INTERNET_SERVICE_FTP ;
  44. pch = (char *) &szURL[6];
  45. } else {
  46. dwService = INTERNET_SERVICE_HTTP;
  47. pch = (char *) &szURL[0];
  48. }
  49. // if none of the above, assump http;
  50. // copy the Doman Name
  51. pchT = szDomanName;
  52. while(*pch != '/' && *pch != ':' && *pch != 0)
  53. *pchT++ = *pch++;
  54. *pchT = 0;
  55. // parse out the port number
  56. szPort[0] = 0;
  57. if(*pch == ':') {
  58. pchT = szPort;
  59. pch++; // get past the :
  60. while(*pch != '/' && *pch != 0)
  61. *pchT++ = *pch++;
  62. *pchT = 0;
  63. }
  64. // Get port #, zero is INTERNET_INVALID_PORT_NUMBER
  65. if(szPort[0] != 0)
  66. dwPort = atol(szPort);
  67. // save away what to look up.
  68. if(NULL == (szPartURL = (char *) malloc(sizeof(char) * (strlen(pch) + 1)))) {
  69. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  70. goto ErrorReturn;
  71. }
  72. strcpy(szPartURL, pch);
  73. // INTERNET_OPENLYPE_DIRECT,
  74. if( (hIOpen = InternetOpenA( "Transport",
  75. INTERNET_OPEN_TYPE_PRECONFIG,
  76. NULL,
  77. NULL,
  78. 0)) == NULL ||
  79. (hIConnect = InternetConnectA(hIOpen,
  80. szDomanName,
  81. (INTERNET_PORT) dwPort,
  82. NULL,
  83. NULL,
  84. dwService,
  85. 0,
  86. 0)) == NULL ) {
  87. goto ErrorReturn;
  88. }
  89. // If this is a GET, do a dummy send
  90. if( ((hIHttp = HttpOpenRequestA(hIConnect,
  91. "GET",
  92. szPartURL,
  93. HTTP_VERSION,
  94. NULL,
  95. NULL,
  96. INTERNET_FLAG_DONT_CACHE,
  97. 0)) == NULL ||
  98. HttpSendRequestA(hIHttp, "Accept: */*\r\n", (DWORD) -1, NULL, 0) == FALSE) ) {
  99. goto ErrorReturn;
  100. }
  101. cbBuff = sizeof(szBuff);
  102. if(HttpQueryInfoA( hIHttp,
  103. HTTP_QUERY_CONTENT_TYPE,
  104. szBuff,
  105. &cbBuff,
  106. NULL) == FALSE)
  107. goto ErrorReturn;
  108. assert(cbBuff > 0);
  109. // now get the length of the buffer returned
  110. cbBuff = sizeof(szLong);
  111. if(HttpQueryInfo( hIHttp,
  112. HTTP_QUERY_CONTENT_LENGTH,
  113. szLong,
  114. &cbBuff,
  115. NULL) == FALSE)
  116. goto ErrorReturn;
  117. assert(cbBuff > 0);
  118. // always appears to be in ascii
  119. cbBuff = atol(szLong);
  120. // allocate a buffer
  121. if( (pbRecBuf = (BYTE *) malloc(cbBuff)) == NULL ) {
  122. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  123. goto ErrorReturn;
  124. }
  125. // read the data
  126. cbBuffRead = 0;
  127. while(cbBuffRead < cbBuff) {
  128. cbBuffT = 0;
  129. if(InternetReadFile(hIHttp, &pbRecBuf[cbBuffRead], (cbBuff - cbBuffRead), &cbBuffT) == FALSE )
  130. goto ErrorReturn;
  131. cbBuffRead += cbBuffT;
  132. }
  133. // close out the handle
  134. InternetCloseHandle(hIHttp);
  135. hIHttp = NULL;
  136. // pass back the info
  137. *pcbReceiveBuff = cbBuff;
  138. CommonReturn:
  139. if(szPartURL != NULL)
  140. free(szPartURL);
  141. if(szURL != NULL)
  142. FreeMBStr(NULL, szURL);
  143. return(pbRecBuf);
  144. ErrorReturn:
  145. if(pbRecBuf != NULL)
  146. free(pbRecBuf);
  147. pbRecBuf = NULL;
  148. goto CommonReturn;
  149. }