2 * Copyright 2010-2014 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Christophe Huriaux, c.huriaux@gmail.com
7 * Niels Sascha Reedijk, niels.reedijk@gmail.com
8 * Adrien Destugues, pulkomandy@pulkomandy.tk
12 #include <NetworkRequest.h>
14 #include <AbstractSocket.h>
17 BNetworkRequest::BNetworkRequest(const BUrl
& url
, BUrlProtocolListener
* listener
,
18 BUrlContext
* context
, const char* threadName
, const char* protocolName
)
20 BUrlRequest(url
, listener
, context
, threadName
, protocolName
),
27 BNetworkRequest::Stop()
29 status_t threadStatus
= BUrlRequest::Stop();
31 if (threadStatus
!= B_OK
)
34 send_signal(fThreadId
, SIGUSR1
); // unblock blocking syscalls.
35 wait_for_thread(fThreadId
, &threadStatus
);
41 BNetworkRequest::SetTimeout(bigtime_t timeout
)
44 fSocket
->SetTimeout(timeout
);
49 BNetworkRequest::_ResolveHostName(BString host
, uint16_t port
)
51 _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT
, "Resolving %s",
52 fUrl
.UrlString().String());
54 fRemoteAddr
= BNetworkAddress(host
, port
);
55 if (fRemoteAddr
.InitCheck() != B_OK
)
58 //! ProtocolHook:HostnameResolved
59 if (fListener
!= NULL
)
60 fListener
->HostnameResolved(this, fRemoteAddr
.ToString().String());
62 _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT
, "Hostname resolved to: %s",
63 fRemoteAddr
.ToString().String());
76 BNetworkRequest::_ProtocolSetup()
78 // Setup an (empty) signal handler so we can be stopped by a signal,
79 // without the whole process being killed.
80 // TODO make connect() properly unlock when close() is called on the
81 // socket, and remove this.
82 struct sigaction action
;
83 action
.sa_handler
= empty
;
86 sigaction(SIGUSR1
, &action
, NULL
);
91 BNetworkRequest::_GetLine(BString
& destString
)
93 // Find a complete line in inputBuffer
94 uint32 characterIndex
= 0;
96 while ((characterIndex
< fInputBuffer
.Size())
97 && ((fInputBuffer
.Data())[characterIndex
] != '\n'))
100 if (characterIndex
== fInputBuffer
.Size())
103 char* temporaryBuffer
= new(std::nothrow
) char[characterIndex
+ 1];
104 if (temporaryBuffer
== NULL
)
107 fInputBuffer
.RemoveData(temporaryBuffer
, characterIndex
+ 1);
109 // Strip end-of-line character(s)
110 if (temporaryBuffer
[characterIndex
- 1] == '\r')
111 destString
.SetTo(temporaryBuffer
, characterIndex
- 1);
113 destString
.SetTo(temporaryBuffer
, characterIndex
);
115 delete[] temporaryBuffer
;