Add file: protocol handler.
This commit is contained in:
@@ -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 <deque>
|
||||||
|
|
||||||
|
|
||||||
|
#include <UrlRequest.h>
|
||||||
|
|
||||||
|
|
||||||
|
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_
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 Haiku Inc. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* Adrien Destugues, [email protected]
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <new>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include <File.h>
|
||||||
|
#include <FileRequest.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -58,8 +58,12 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
|||||||
HttpHeaders.cpp
|
HttpHeaders.cpp
|
||||||
HttpForm.cpp
|
HttpForm.cpp
|
||||||
HttpRequest.cpp
|
HttpRequest.cpp
|
||||||
|
HttpResult.cpp
|
||||||
HttpTime.cpp
|
HttpTime.cpp
|
||||||
|
|
||||||
|
# TODO: another add-on for file:// (a much simpler one)
|
||||||
|
FileRequest.cpp
|
||||||
|
|
||||||
$(md5Sources)
|
$(md5Sources)
|
||||||
|
|
||||||
Url.cpp
|
Url.cpp
|
||||||
@@ -69,7 +73,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
|||||||
UrlProtocolListener.cpp
|
UrlProtocolListener.cpp
|
||||||
UrlProtocolRoster.cpp
|
UrlProtocolRoster.cpp
|
||||||
UrlRequest.cpp
|
UrlRequest.cpp
|
||||||
UrlResult.cpp
|
|
||||||
UrlSynchronousRequest.cpp
|
UrlSynchronousRequest.cpp
|
||||||
|
|
||||||
:
|
:
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
#include <UrlRequest.h>
|
#include <UrlRequest.h>
|
||||||
|
#include <FileRequest.h>
|
||||||
#include <HttpRequest.h>
|
#include <HttpRequest.h>
|
||||||
#include <Debug.h>
|
#include <Debug.h>
|
||||||
|
|
||||||
@@ -27,7 +28,12 @@ BUrlProtocolRoster::MakeRequest(const BUrl& url,
|
|||||||
} else if (url.Protocol() == "https") {
|
} else if (url.Protocol() == "https") {
|
||||||
return new(std::nothrow) BHttpRequest(url, true, "HTTPS", listener,
|
return new(std::nothrow) BHttpRequest(url, true, "HTTPS", listener,
|
||||||
context);
|
context);
|
||||||
|
} else if (url.Protocol() == "file") {
|
||||||
|
puts("*** FILE URL");
|
||||||
|
return new(std::nothrow) BFileRequest(url, listener, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
puts("*** UNKNOWN protocol");
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user