+//------------------------------------------------------------------------------
+unsigned short Request::port() const
+{
+ std::string hp(hostAndPort());
+ int colonPos = hp.find(':');
+ if (colonPos >= 0) {
+ return (unsigned short) strutils::to_int(hp.substr(colonPos + 1));
+ } else {
+ return DEFAULT_HTTP_PORT;
+ }
+}
+
+//------------------------------------------------------------------------------
+std::string Request::hostAndPort() const
+{
+ std::string u(url());
+ int schemeEnd = u.find("://");
+ if (schemeEnd < 0) {
+ return ""; // couldn't parse scheme
+ }
+
+ int hostEnd = u.find('/', schemeEnd + 3);
+ if (hostEnd < 0) { // all remainder of URL is host
+ return u.substr(schemeEnd + 3);
+ }
+
+ return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
+}
+
+//------------------------------------------------------------------------------
+std::string Request::responseMime() const
+{
+ std::string content_type = _responseHeaders.get("content-type");
+ if( content_type.empty() )
+ return "application/octet-stream";
+
+ return content_type.substr(0, content_type.find(';'));
+}
+
+//------------------------------------------------------------------------------
+void Request::setResponseLength(unsigned int l)
+{
+ _responseLength = l;
+}
+
+//------------------------------------------------------------------------------
+unsigned int Request::responseLength() const
+{
+ // if the server didn't supply a content length, use the number
+ // of bytes we actually received (so far)
+ if( (_responseLength == 0) && (_receivedBodyBytes > 0) )
+ return _receivedBodyBytes;
+
+ return _responseLength;
+}
+
+//------------------------------------------------------------------------------
+void Request::setFailure(int code, const std::string& reason)
+{
+ SG_LOG(SG_IO, SG_WARN, "HTTP request: set failure:" << code << " reason " << reason);
+ _responseStatus = code;
+ _responseReason = reason;
+
+ if( !isComplete() )
+ setReadyState(FAILED);
+}
+
+//------------------------------------------------------------------------------
+void Request::setReadyState(ReadyState state)
+{
+ _ready_state = state;
+ if( state == DONE )
+ {
+ // Finish C++ part of request to ensure everything is finished (for example
+ // files and streams are closed) before calling any callback (possibly using
+ // such files)
+ onDone();
+ onAlways();
+
+ _cb_done(this);
+ }
+ else if( state == FAILED )
+ {
+ onFail();
+ onAlways();
+
+ _cb_fail(this);
+ }
+ else
+ return;