diff --git a/CefSharp/SchemeHandlerResponse.h b/CefSharp/SchemeHandlerResponse.h index a78e14eaf..f7e558f77 100644 --- a/CefSharp/SchemeHandlerResponse.h +++ b/CefSharp/SchemeHandlerResponse.h @@ -26,8 +26,33 @@ namespace CefSharp property String^ MimeType; property IDictionary^ ResponseHeaders; + /// + /// The status code of the response. Unless set, the default value used is 200 + /// (corresponding to HTTP status OK). + /// + property int StatusCode; + + /// + /// The length of the response contents. Defaults to -1, which means unknown length + /// and causes CefSharp to read the response stream in pieces. Thus, setting a length + /// is optional but allows for more optimal response reading. + /// + property int ContentLength; + + /// + /// URL to redirect to (leave empty to not redirect). + /// + property String^ RedirectUrl; + + /// + /// Set to true to close the response stream once it has been read. The default value + /// is false in order to preserve the old CefSharp behavior of not closing the stream. + /// + property bool CloseStream; + SchemeHandlerResponse(SchemeHandlerWrapper* schemeHandlerWrapper) { + ContentLength = -1; _schemeHandlerWrapper = new CefRefPtr(schemeHandlerWrapper); } diff --git a/CefSharp/SchemeHandlerWrapper.cpp b/CefSharp/SchemeHandlerWrapper.cpp index 7b6e320a2..23e35724d 100644 --- a/CefSharp/SchemeHandlerWrapper.cpp +++ b/CefSharp/SchemeHandlerWrapper.cpp @@ -52,6 +52,10 @@ namespace CefSharp { _mime_type = toNative(response->MimeType); _stream = response->ResponseStream; + _statusCode = response->StatusCode; + _redirectUrl = toNative(response->RedirectUrl); + _contentLength = response->ContentLength; + _closeStream = response->CloseStream; _headers = ToHeaderMap(response->ResponseHeaders); @@ -65,9 +69,17 @@ namespace CefSharp void SchemeHandlerWrapper::GetResponseHeaders(CefRefPtr response, int64& response_length, CefString& redirectUrl) { response->SetMimeType(_mime_type); - response->SetStatus(200); + response->SetStatus(_statusCode > 0 ? _statusCode : 200); response->SetHeaderMap(_headers); - response_length = SizeFromStream(); + if (_contentLength >= 0) + { + response_length = _contentLength; + } + else + { + response_length = SizeFromStream(); + } + redirectUrl = _redirectUrl; } bool SchemeHandlerWrapper::ReadResponse(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr callback) @@ -87,7 +99,12 @@ namespace CefSharp pin_ptr src = &buffer[0]; memcpy(data_out, static_cast(src), ret); bytes_read = ret; - has_data = true; + // must return false when the response is complete + has_data = ret > 0; + if (!has_data && _closeStream) + { + _stream->Close(); + } } return has_data; @@ -95,6 +112,10 @@ namespace CefSharp void SchemeHandlerWrapper::Cancel() { + if (!!_stream && _closeStream) + { + _stream->Close(); + } _stream = nullptr; } diff --git a/CefSharp/SchemeHandlerWrapper.h b/CefSharp/SchemeHandlerWrapper.h index 84e78c357..47a5ce6b9 100644 --- a/CefSharp/SchemeHandlerWrapper.h +++ b/CefSharp/SchemeHandlerWrapper.h @@ -24,6 +24,10 @@ namespace CefSharp CefString _mime_type; CefResponse::HeaderMap _headers; CefRefPtr _callback; + int _statusCode; + CefString _redirectUrl; + int _contentLength; + bool _closeStream; int SizeFromStream();