void readsome(const boost::system::error_code& ec, std::size_t bytes_transferred) { if (ec == error::eof) { file.close(); if (!file) { ERROR "Cannot write to file " << filename; onerror(); } else if (filesize == 0) { ERROR "Empty segment " << file_url.to_string(); onerror(); } else { downloading = false; complete = true; auto download_duration = duration_cast<milliseconds>(steady_clock::now() - download_start).count(); INFO "Completed segment " << file_url.to_string() << " (" << std::setprecision(1) << std::fixed << (filesize / 1024.0 / 1024.0) << " MB) in " << download_duration << " ms (" << (filesize * 8000 / 1024.0 / 1024.0 / download_duration) << "Mbps)"; } } else if (ec) { ERROR "Error reading URL " << file_url.to_string() << ": " << ec.message(); onerror(); } else { file.write(&buffer[0], bytes_transferred); filesize += bytes_transferred; segment_stream.async_read_some(boost::asio::buffer(buffer), std::bind(&Segment::readsome, this, _1, _2)); } }
natus_pacrunner(string pac, const url& pacurl) throw (bad_alloc) : pacrunner(pac, pacurl) { Value exc; // Create the basic context if (!eng.initialize()) goto error; glb = this->eng.newGlobal(); if (glb.isException()) goto error; // Add dnsResolve into the context if (!glb.set("dnsResolve", glb.newFunction(dnsResolve))) goto error; // Add myIpAddress into the context if (!glb.set("myIpAddress", glb.newFunction(myIpAddress))) goto error; // Add all other routines into the context exc = glb.evaluate(JAVASCRIPT_ROUTINES); if (exc.isException()) goto error; // Add the PAC into the context exc = glb.evaluate(pac.c_str(), pacurl.to_string()); if (exc.isException()) goto error; return; error: throw bad_alloc(); }
string run(const url& url_) throw (bad_alloc) { vector<Value> args; args.push_back(glb.newString(url_.to_string())); args.push_back(glb.newString(url_.get_host())); Value res = glb.call("FindProxyForURL", args); if (res.isString() && !res.isException()) return res.toString(); return ""; }
void websocket::connect(url const& target) { state_ = CONNECTING; if ( target.scheme() != "ws" && target.scheme() != "wss" ) { throw std::invalid_argument("Unsupported URL " + target.to_string()); } tcp_conn_.reset(new pion::tcp::connection(rt_.io_service(), target.is_scheme_secured())); // tcp_conn_->getSocket().set_option(boost::asio::ip::tcp::no_delay(true)); // disable ngale // create resolver and start async resolve boost::shared_ptr<tcp::resolver> resolver = boost::make_shared<tcp::resolver>(rt_.io_service()); tcp::resolver::query query(target.host(), std::to_string(static_cast<unsigned long long>(target.effective_port())), tcp::resolver::query::numeric_service); resolver->async_resolve(query, boost::bind(&websocket::handle_resolve, this, target, resolver, boost::asio::placeholders::error, boost::asio::placeholders::iterator)); }
string run(const url& url_) throw (bad_alloc) { JSStringRef str = NULL; JSValueRef val = NULL; string tmp; // Run the PAC tmp = string("FindProxyForURL(\"") + url_.to_string() + string("\", \"") + url_.get_host() + "\");"; str = JSStringCreateWithUTF8CString(tmp.c_str()); if (!str) throw bad_alloc(); if (!JSCheckScriptSyntax(this->jsctx, str, NULL, 0, NULL)) goto error; if (!(val = JSEvaluateScript(this->jsctx, str, NULL, NULL, 1, NULL))) goto error; if (!JSValueIsString(this->jsctx, val)) goto error; JSStringRelease(str); // Convert the return value to a string return jstr2str(JSValueToStringCopy(this->jsctx, val, NULL), true); error: JSStringRelease(str); return ""; }
bool url::operator==(const url& url) const { return m_orig == url.to_string(); }
bool websocket_hybi_17::handshake(url const& target) { _aspect_assert(state() == CONNECTING); // create websocket handshake request pion::http::request req(target.path_for_request()); req.add_header("Connection", "Upgrade"); req.add_header("Upgrade", "WebSocket"); req.add_header("Host", target.hostport()); req.add_header("Origin", target.origin()); string key(16, 0); std::generate(key.begin(), key.end(), rand_byte); string encoded_key; if ( !pion::algorithm::base64_encode(key, encoded_key) ) { return false; } req.add_header("Sec-WebSocket-Key", encoded_key); req.add_header("Sec-WebSocket-Version", "13"); // send request, recieve response error_code err = send_message(req); if ( !check_err(err, "send request") ) { return false; } pion::http::response resp(req); resp.receive(*tcp_conn_, err); if ( !check_err(err, "recieve response") ) { return false; } // check response if ( !is_websocket_upgrade(resp) ) { // it is not a hybi connection upgrade, give a chance for another protocol return false; } // Check reply key encoded_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; encoded_key = crypto::sha1_digest(encoded_key.data(), encoded_key.size()); string resp_key; if ( !pion::algorithm::base64_encode(encoded_key, resp_key) ) { return false; } if ( resp_key != resp.get_header("Sec-WebSocket-Accept") ) { return false; } state_ = OPEN; on_connect(target.to_string()); wait_data(); return true; }