// Copyright © 2010-2015 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. #include "Stdafx.h" #include "Internals/CefRequestWrapper.h" #include "Internals/CefResponseWrapper.h" #include "Internals/CefCallbackWrapper.h" #include "ResourceHandlerWrapper.h" #include "Internals/TypeConversion.h" using namespace System::Runtime::InteropServices; using namespace System::IO; namespace CefSharp { bool ResourceHandlerWrapper::ProcessRequest(CefRefPtr request, CefRefPtr callback) { _callbackWrapper = gcnew CefCallbackWrapper(callback); _request = gcnew CefRequestWrapper(request); AutoLock lock_scope(_syncRoot); return _handler->ProcessRequestAsync(_request, _callbackWrapper); } void ResourceHandlerWrapper::GetResponseHeaders(CefRefPtr response, int64& response_length, CefString& redirectUrl) { String^ newRedistUrl; CefResponseWrapper responseWrapper(response); _stream = _handler->GetResponse(%responseWrapper, response_length, newRedistUrl); redirectUrl = StringUtils::ToNative(newRedistUrl); } bool ResourceHandlerWrapper::ReadResponse(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr callback) { bool hasData = false; AutoLock lock_scope(_syncRoot); if (static_cast(_stream) == nullptr) { bytes_read = 0; } else { array^ buffer = gcnew array(bytes_to_read); bytes_read = _stream->Read(buffer, 0, bytes_to_read); pin_ptr src = &buffer[0]; memcpy(data_out, static_cast(src), bytes_read); // must return false when the response is complete hasData = bytes_read > 0; //TODO: Fix this /*if (!hasData && _closeStream) { _stream->Close(); }*/ } return hasData; } void ResourceHandlerWrapper::Cancel() { //TODO: Fix this /*if (static_cast(_stream) != nullptr && _closeStream) { _stream->Close(); }*/ _stream = nullptr; delete this; } int64 ResourceHandlerWrapper::SizeFromStream() { if (static_cast(_stream) == nullptr) { return 0; } if (_stream->CanSeek) { _stream->Seek(0, System::IO::SeekOrigin::End); int64 length = static_cast(_stream->Position); _stream->Seek(0, System::IO::SeekOrigin::Begin); return length; } return -1; } }