2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Christophe Huriaux, c.huriaux@gmail.com
10 #include <UrlRequest.h>
15 static BReference
<BUrlContext
> gDefaultContext
= new(std::nothrow
) BUrlContext();
18 BUrlRequest::BUrlRequest(const BUrl
& url
, BUrlProtocolListener
* listener
,
19 BUrlContext
* context
, const char* threadName
, const char* protocolName
)
26 fThreadStatus(B_NO_INIT
),
28 fThreadName(threadName
),
29 fProtocol(protocolName
)
32 fContext
= gDefaultContext
;
36 BUrlRequest::~BUrlRequest()
42 // #pragma mark URL protocol thread management
48 // Thread already running
50 PRINT(("BUrlRequest::Run() : Oops, already running ! "
51 "[urlProtocol=%p]!\n", this));
55 fThreadId
= spawn_thread(BUrlRequest::_ThreadEntry
, fThreadName
,
56 B_NORMAL_PRIORITY
, this);
63 status_t launchErr
= resume_thread(fThreadId
);
64 if (launchErr
< B_OK
) {
65 PRINT(("BUrlRequest::Run() : Failed to resume thread %" B_PRId32
"\n",
101 // #pragma mark URL protocol parameters modification
105 BUrlRequest::SetUrl(const BUrl
& url
)
107 // We should avoid to change URL while the thread is running ...
117 BUrlRequest::SetContext(BUrlContext
* context
)
128 BUrlRequest::SetListener(BUrlProtocolListener
* listener
)
133 fListener
= listener
;
138 // #pragma mark URL protocol parameters access
142 BUrlRequest::Url() const
149 BUrlRequest::Context() const
155 BUrlProtocolListener
*
156 BUrlRequest::Listener() const
163 BUrlRequest::Protocol() const
169 // #pragma mark URL protocol informations
173 BUrlRequest::IsRunning() const
180 BUrlRequest::Status() const
182 return fThreadStatus
;
186 // #pragma mark Thread management
190 BUrlRequest::_ThreadEntry(void* arg
)
192 BUrlRequest
* request
= reinterpret_cast<BUrlRequest
*>(arg
);
193 request
->fThreadStatus
= B_BUSY
;
194 request
->_ProtocolSetup();
196 status_t protocolLoopExitStatus
= request
->_ProtocolLoop();
198 request
->fRunning
= false;
199 request
->fThreadStatus
= protocolLoopExitStatus
;
201 if (request
->fListener
!= NULL
) {
202 request
->fListener
->RequestCompleted(request
,
203 protocolLoopExitStatus
== B_OK
);
211 BUrlRequest::_EmitDebug(BUrlProtocolDebugMessage type
,
212 const char* format
, ...)
214 if (fListener
== NULL
)
218 va_start(arguments
, format
);
221 vsnprintf(debugMsg
, sizeof(debugMsg
), format
, arguments
);
222 fListener
->DebugMessage(this, type
, debugMsg
);