From c52c444c2733032679528e83572e8f2f6d576a13 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 24 Oct 2013 17:31:25 +0200 Subject: [PATCH] Add file: protocol handler. --- headers/os/net/FileRequest.h | 29 ++++++++++ src/kits/network/libnetapi/FileRequest.cpp | 56 +++++++++++++++++++ src/kits/network/libnetapi/Jamfile | 5 +- .../network/libnetapi/UrlProtocolRoster.cpp | 6 ++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 headers/os/net/FileRequest.h create mode 100644 src/kits/network/libnetapi/FileRequest.cpp diff --git a/headers/os/net/FileRequest.h b/headers/os/net/FileRequest.h new file mode 100644 index 0000000000..49c5974926 --- /dev/null +++ b/headers/os/net/FileRequest.h @@ -0,0 +1,29 @@ +/* + * Copyright 2013 Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _B_FILE_REQUEST_H_ +#define _B_FILE_REQUEST_H_ + + +#include + + +#include + + +class BFileRequest : public BUrlRequest { +public: + BFileRequest(const BUrl& url, + BUrlProtocolListener* listener = NULL, + BUrlContext* context = NULL); + virtual ~BFileRequest(); + + void SetDisableListener(bool disable); + +private: + status_t _ProtocolLoop(); +}; + + +#endif // _B_FILE_REQUEST_H_ diff --git a/src/kits/network/libnetapi/FileRequest.cpp b/src/kits/network/libnetapi/FileRequest.cpp new file mode 100644 index 0000000000..a57b15525d --- /dev/null +++ b/src/kits/network/libnetapi/FileRequest.cpp @@ -0,0 +1,56 @@ +/* + * Copyright 2013 Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Adrien Destugues, pulkomandy@pulkomandy.tk + */ + + +#include +#include + +#include + +#include +#include + + +BFileRequest::BFileRequest(const BUrl& url, BUrlProtocolListener* listener, + BUrlContext* context) + : + BUrlRequest(url, listener, context, "BUrlProtocol.File", "file") +{ +} + + +BFileRequest::~BFileRequest() +{ +} + + +status_t +BFileRequest::_ProtocolLoop() +{ + // FIXME error handling (file does not exists, etc.) + BFile file(fUrl.Path().String(), B_READ_ONLY); + + if(file.InitCheck() != B_OK) + return B_PROT_CONNECTION_FAILED; + + // Send all notifications to listener, if any + if (fListener != NULL) { + fListener->ConnectionOpened(this); + off_t size = 0; + file.GetSize(&size); + fListener->DownloadProgress(this, size, size); + + size_t chunkSize; + char chunk[4096]; + while((chunkSize = file.Read(chunk, sizeof(chunk))) > 0) + fListener->DataReceived(this, chunk, chunkSize); + } + + return B_PROT_SUCCESS; +} + diff --git a/src/kits/network/libnetapi/Jamfile b/src/kits/network/libnetapi/Jamfile index ec079c279b..bea137c245 100644 --- a/src/kits/network/libnetapi/Jamfile +++ b/src/kits/network/libnetapi/Jamfile @@ -58,8 +58,12 @@ for architectureObject in [ MultiArchSubDirSetup ] { HttpHeaders.cpp HttpForm.cpp HttpRequest.cpp + HttpResult.cpp HttpTime.cpp + # TODO: another add-on for file:// (a much simpler one) + FileRequest.cpp + $(md5Sources) Url.cpp @@ -69,7 +73,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { UrlProtocolListener.cpp UrlProtocolRoster.cpp UrlRequest.cpp - UrlResult.cpp UrlSynchronousRequest.cpp : diff --git a/src/kits/network/libnetapi/UrlProtocolRoster.cpp b/src/kits/network/libnetapi/UrlProtocolRoster.cpp index 2e2a6c917f..830bd2bafb 100644 --- a/src/kits/network/libnetapi/UrlProtocolRoster.cpp +++ b/src/kits/network/libnetapi/UrlProtocolRoster.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -27,7 +28,12 @@ BUrlProtocolRoster::MakeRequest(const BUrl& url, } else if (url.Protocol() == "https") { return new(std::nothrow) BHttpRequest(url, true, "HTTPS", listener, context); + } else if (url.Protocol() == "file") { + puts("*** FILE URL"); + return new(std::nothrow) BFileRequest(url, listener, context); } + puts("*** UNKNOWN protocol"); + return NULL; }