| 716 | } |
| 717 | |
| 718 | static bool BaseHttp_Read( |
| 719 | TFileStream * pStream, // Pointer to an open stream |
| 720 | ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position |
| 721 | void * pvBuffer, // Pointer to data to be read |
| 722 | DWORD dwBytesToRead) // Number of bytes to read from the file |
| 723 | { |
| 724 | #ifdef PLATFORM_WINDOWS |
| 725 | ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.Http.FilePos; |
| 726 | DWORD dwTotalBytesRead = 0; |
| 727 | |
| 728 | // Do we have to read anything at all? |
| 729 | if(dwBytesToRead != 0) |
| 730 | { |
| 731 | HINTERNET hRequest; |
| 732 | LPCTSTR szFileName; |
| 733 | LPBYTE pbBuffer = (LPBYTE)pvBuffer; |
| 734 | TCHAR szRangeRequest[0x80]; |
| 735 | DWORD dwStartOffset = (DWORD)ByteOffset; |
| 736 | DWORD dwEndOffset = dwStartOffset + dwBytesToRead; |
| 737 | |
| 738 | // Open HTTP request to the file |
| 739 | szFileName = BaseHttp_ExtractServerName(pStream->szFileName, NULL); |
| 740 | hRequest = HttpOpenRequest(pStream->Base.Http.hConnect, _T("GET"), szFileName, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); |
| 741 | if(hRequest != NULL) |
| 742 | { |
| 743 | // Add range request to the HTTP headers |
| 744 | // http://www.clevercomponents.com/articles/article015/resuming.asp |
| 745 | _stprintf(szRangeRequest, _T("Range: bytes=%u-%u"), (unsigned int)dwStartOffset, (unsigned int)dwEndOffset); |
| 746 | HttpAddRequestHeaders(hRequest, szRangeRequest, 0xFFFFFFFF, HTTP_ADDREQ_FLAG_ADD_IF_NEW); |
| 747 | |
| 748 | // Send the request to the server |
| 749 | if(HttpSendRequest(hRequest, NULL, 0, NULL, 0)) |
| 750 | { |
| 751 | while(dwTotalBytesRead < dwBytesToRead) |
| 752 | { |
| 753 | DWORD dwBlockBytesToRead = dwBytesToRead - dwTotalBytesRead; |
| 754 | DWORD dwBlockBytesRead = 0; |
| 755 | |
| 756 | // Read the block from the file |
| 757 | if(dwBlockBytesToRead > 0x200) |
| 758 | dwBlockBytesToRead = 0x200; |
| 759 | InternetReadFile(hRequest, pbBuffer, dwBlockBytesToRead, &dwBlockBytesRead); |
| 760 | |
| 761 | // Check for end |
| 762 | if(dwBlockBytesRead == 0) |
| 763 | break; |
| 764 | |
| 765 | // Move buffers |
| 766 | dwTotalBytesRead += dwBlockBytesRead; |
| 767 | pbBuffer += dwBlockBytesRead; |
| 768 | } |
| 769 | } |
| 770 | InternetCloseHandle(hRequest); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Increment the current file position by number of bytes read |
| 775 | pStream->Base.Http.FilePos = ByteOffset + dwTotalBytesRead; |
nothing calls this directly
no test coverage detected