int StHlsTask::DownloadTS(StHttpClient& client, M3u8TS& ts){ int ret = ERROR_SUCCESS; HttpUrl url; if((ret = url.Initialize(ts.ts_url)) != ERROR_SUCCESS){ Error("initialize ts url failed. ret=%d", ret); return ret; } Info("[TS] url=%s, duration=%.2f, delay=%.2f", url.GetUrl(), ts.duration, delay_seconds); statistic->OnSubTaskStart(GetId(), ts.ts_url); if((ret = client.DownloadString(&url, NULL)) != ERROR_SUCCESS){ statistic->OnSubTaskError(GetId(), (int)ts.duration); Error("http client download ts file %s failed. ret=%d", url.GetUrl(), ret); return ret; } int sleep_ms = StUtility::BuildRandomMTime((delay_seconds >= 0)? delay_seconds:ts.duration); Trace("[TS] url=%s download, duration=%.2f, delay=%.2f, size=%"PRId64", sleep %dms", url.GetUrl(), ts.duration, delay_seconds, client.GetResponseHeader()->content_length, sleep_ms); st_usleep(sleep_ms * 1000); statistic->OnSubTaskEnd(GetId(), (int)ts.duration); return ret; }
YETI_Result HttpTcpConnector::connect(const HttpUrl & url, HttpClient & client, const HttpProxyAddress * proxy, bool reuse, HttpClient::Connection *& connection) { connection = NULL; const char * server_hostname; YETI_UInt16 server_port; if (proxy) { server_hostname = (const char *)proxy->get_hostname(); server_port = proxy->get_port(); } else { server_hostname = (const char *)url.get_host(); server_port = url.get_port(); } IpAddress address; YETI_CHECK_FINE(address.resolve_name(server_hostname, client.get_config().m_name_resolver_timeout_)); YETI_LOG_FINE_2("TCP connector will connect to %s : %d", server_hostname, server_port); TcpClientSocket * tcp_socket = new TcpClientSocket(); SocketReference socket(tcp_socket, true); tcp_socket->set_read_timeout(client.get_config().m_io_timeout_); tcp_socket->set_write_timeout(client.get_config().m_io_timeout_); SocketAddress socket_address(address, server_port); YETI_CHECK_FINE(tcp_socket->connect(socket_address, client.get_config().m_connection_timeout_)); HttpSimpleConnection * _connection = new HttpSimpleConnection(); _connection->m_socket_ = socket; connection = _connection; tcp_socket->get_input_stream(_connection->m_inputstream_); tcp_socket->get_output_stream(_connection->m_outputstream_); return YETI_SUCCESS; }
HttpUrl* HttpUrl::Copy(){ HttpUrl* copy = new HttpUrl(); copy->Initialize(url); return copy; }
void HttpRequest::setUrl(const HttpUrl &url) { m_rawurl = url.getPath(); m_extra = url.getParams(); setHost(url.getHost()); setPort(url.getPort()); }
YETI_Result HttpEnvProxySelector::get_proxy_for_url(const HttpUrl & url, HttpProxyAddress & proxy) { HttpProxyAddress * protocol_proxy = NULL; switch (url.get_schemeid()) { case Uri::SCHEME_ID_HTTP: protocol_proxy = &m_http_proxy_; break; case Uri::SCHEME_ID_HTTPS: protocol_proxy = &m_https_proxy_; break; default: return YETI_ERROR_HTTP_NO_PROXY; } if (m_no_proxy_.get_item_count()) { for (List<String>::iterator i = m_no_proxy_.get_first_item(); i; ++i) { if ((*i) == "*") { return YETI_ERROR_HTTP_NO_PROXY; } if (url.get_host().ends_with(*i, true)) { if (url.get_host().get_length() == (*i).get_length()) { return YETI_ERROR_HTTP_NO_PROXY; } if (url.get_host().get_chars()[url.get_host().get_length() - (*i).get_length() - 1] == '.') { return YETI_ERROR_HTTP_NO_PROXY; } } } } if (protocol_proxy->get_hostname().get_length()) { proxy = *protocol_proxy; return YETI_SUCCESS; } proxy = m_all_proxy_; return proxy.get_hostname().get_length() ? YETI_SUCCESS : YETI_ERROR_HTTP_NO_PROXY; }
// dump request void dump_request( HttpRequest& req, HttpUrl& url, CSocket& s ) { s.printf( "<br><br><table border width=650>" ); s.printf( "<tr><td>%s</td><td>%s</td></tr>", req.method(), url.file() ); for( HttpRequest::TagsIterator it=req.tags_begin(); it != req.tags_end(); it++ ) { s.printf( "<tr><td><b>%s</b></td>", (*it).first.c_str() ); if( !(*it).second.empty() ) s.printf( "<td>%s</td></tr>", (char*)(*it).second ); } s.printf("</table>" ); // informatiile din url s.printf( "<table border width=650>" ); for( HttpUrl::QueryIterator itr=url.query_begin(); itr != url.query_end(); itr++ ) { s.printf( "<tr><td><b>%s</b></td>", (*itr).first.c_str() ); if( !(*itr).second.empty() ) s.printf( "<td>%s</td></tr>", (char*)(*itr).second ); } s.printf("</table>" ); }
/** This is the main function from the HTTP server. Every new connection from a client is handled here. Here the request from the client is handled. */ void HTTPServer( GN::ClientList&, GN::CSocket & s ) { try { // buffer pentru cererea HTTP char buffer[ MAX_REQUEST_RECEIVE_BUFFER+2 ]; HttpRequest req; // Clasa pentru procesare cerere HTTP HttpUrl url; // Clasa pentru procesare URL HttpResponse r; // Clasa pentru raspunsul HTTP // receptioneaza cerere int ret = s.receive( buffer, MAX_REQUEST_RECEIVE_BUFFER ); buffer[ret]=0; // proceseaza cererea primita req.parse( buffer, ret ); check_for_errors( s, req ); // parse url url.parse( req.url(), WWW_FOLDER ); // Verifica daca exista fisierul FILE * file = fopen( url.file(), "r"); if( !file ) { r.redirect( STD_404_PAGE ); r.send(s); GSTD::CError error( 0, "File not found"); throw error; } fclose(file); // Trimite raspunsul ok - fisierul exista si continutul va fi trimis r.set_status( HTTP_OK ); r.set("Server", "GCWS 1.0"); r.set("Connection","close"); // incarca DLL // seteaza tipul continutului get_content_type( url.ext(), buffer ); r.set("Content-type", buffer ); // trimite raspunsul cu toate campurile setate corespunzator r.send(s); // send file - one buffer at time file = fopen( url.file(), "rb" ); int nb; do { nb = fread( buffer, 1, MAX_REQUEST_RECEIVE_BUFFER, file ); if( nb ) s.send( buffer, nb ); }while( nb ); fclose( file ); // if( url.is_set("debug") ) dump_request( req, url, s ); } catch( GSTD::CError error ) { throw error; } }
HttpUrl HttpUrl::parse(string url) { HttpUrl httpUrl; uint32_t startPos = HttpUrl::skipLeadingSpace(url, 0, url.length()); uint32_t limitPos = HttpUrl::skipTrailingSpace(url, startPos, url.length()); if (String::regionMatches(url, startPos, string("http:"), 0, 5) == true) { httpUrl.scheme("http"); httpUrl.port(80); }else if (String::regionMatches(url, startPos, string("https:"), 0, 6) == true) { httpUrl.scheme("https"); httpUrl.port(443); } /* host */ url = String::substring(url, startPos, limitPos); cout << "Host:" + url << endl; int32_t hostEnd = String::indexOf(url, '/', httpUrl.scheme().size() + 3); int32_t hostStart = String::indexOf(url, '/', httpUrl.scheme().size() + 2) + 1; if (hostEnd < 0) { httpUrl.host(String::substring(url, hostStart)); return httpUrl; } else httpUrl.host(String::substring(url,hostStart,hostEnd)); /* path */ url = String::substring(url, hostEnd); cout << "Path:" + url << endl; uint32_t slashIndex = 0; for (;;) { int32_t firstSlash = String::indexOf(url, '/', slashIndex); int32_t secondSlash = String::indexOf(url, '/', slashIndex+1); if (secondSlash < 0) { int32_t endPath = HttpUrl::delimiterOffset(url, firstSlash, url.size() - firstSlash, "#?"); if (endPath < 0) { cout << String::substring(url, firstSlash + 1) << endl; httpUrl.addPath(String::substring(url, firstSlash + 1)); return httpUrl; }else{ cout << String::substring(url, firstSlash + 1, endPath) << endl; httpUrl.addPath(String::substring(url, firstSlash + 1, endPath)); url = String::substring(url, endPath); } break; } cout << String::substring(url, firstSlash + 1, secondSlash) << endl; httpUrl.addPath(String::substring(url, firstSlash + 1, secondSlash)); slashIndex = secondSlash; } /* query */ cout << "Query:" << url << endl; int32_t andTokenIndex = 0; int32_t queryStart = 1; int32_t numberSignIndex = 0; for (;;) { andTokenIndex = String::indexOf(url, '&', queryStart); if (andTokenIndex < 0) { numberSignIndex = HttpUrl::delimiterOffset(url, queryStart, url.size() - queryStart, "#"); if (numberSignIndex < 0) { cout << String::substring(url, queryStart) << endl; httpUrl.addQuery(String::substring(url, queryStart)); } else { cout << String::substring(url, queryStart, numberSignIndex) << endl; httpUrl.addQuery(String::substring(url, queryStart, numberSignIndex)); url = String::substring(url, numberSignIndex); } break; } cout << String::substring(url, queryStart, andTokenIndex) << endl; queryStart = andTokenIndex + 1; } // fragment cout << "Fragment:" + url << endl; httpUrl.fragment(String::substring(url, 1)); return std::move(httpUrl); }