本文整理汇总了C++中Url::Resource方法的典型用法代码示例。如果您正苦于以下问题:C++ Url::Resource方法的具体用法?C++ Url::Resource怎么用?C++ Url::Resource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Url
的用法示例。
在下文中一共展示了Url::Resource方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReadAt
size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data) {
s64 absoluteEnd = std::min(absolutePos + (s64)bytes, filesize_);
if (absolutePos >= filesize_ || bytes == 0) {
// Read outside of the file or no read at all, just fail immediately.
return 0;
}
Connect();
char requestHeaders[4096];
// Note that the Range header is *inclusive*.
snprintf(requestHeaders, sizeof(requestHeaders),
"Range: bytes=%lld-%lld\r\n", absolutePos, absoluteEnd - 1);
int err = client_.SendRequest("GET", url_.Resource().c_str(), requestHeaders, nullptr);
if (err < 0) {
Disconnect();
return 0;
}
Buffer readbuf;
std::vector<std::string> responseHeaders;
int code = client_.ReadResponseHeaders(&readbuf, responseHeaders);
if (code != 206) {
ERROR_LOG(LOADER, "HTTP server did not respond with range, received code=%03d", code);
Disconnect();
return 0;
}
// TODO: Expire cache via ETag, etc.
// We don't support multipart/byteranges responses.
bool supportedResponse = false;
for (std::string header : responseHeaders) {
if (startsWithNoCase(header, "Content-Range:")) {
// TODO: More correctness. Whitespace can be missing or different.
s64 first = -1, last = -1, total = -1;
std::string lowerHeader = header;
std::transform(lowerHeader.begin(), lowerHeader.end(), lowerHeader.begin(), tolower);
if (sscanf(lowerHeader.c_str(), "content-range: bytes %lld-%lld/%lld", &first, &last, &total) >= 2) {
if (first == absolutePos && last == absoluteEnd - 1) {
supportedResponse = true;
} else {
ERROR_LOG(LOADER, "Unexpected HTTP range: got %lld-%lld, wanted %lld-%lld.", first, last, absolutePos, absoluteEnd - 1);
}
} else {
ERROR_LOG(LOADER, "Unexpected HTTP range response: %s", header.c_str());
}
}
}
// TODO: Would be nice to read directly.
Buffer output;
int res = client_.ReadResponseEntity(&readbuf, responseHeaders, &output);
if (res != 0) {
ERROR_LOG(LOADER, "Unable to read HTTP response entity: %d", res);
// Let's take anything we got anyway. Not worse than returning nothing?
}
// TODO: Keepalive instead.
Disconnect();
if (!supportedResponse) {
ERROR_LOG(LOADER, "HTTP server did not respond with the range we wanted.");
return 0;
}
size_t readBytes = output.size();
output.Take(readBytes, (char *)data);
filepos_ = absolutePos + readBytes;
return readBytes;
}