示例#1
0
bool http_download::save_range(const char* body, size_t len,
	acl_int64 range_from, acl_int64 range_to)
{
	if (range_from < 0)
	{
		logger_error("invalid range_from: %lld", range_from);
		return false;
	}
	else if (range_to >= 0 && range_to < range_from)
	{
		logger_error("invalid, 0 <= range_to: %lld < range_from: %lld",
			range_to, range_from);
		return false;
	}

	http_method_t method = body && len > 0
		? HTTP_METHOD_POST : HTTP_METHOD_GET;

	// 发送带 range 字段的下载请求

	// 设置 HTTP 请求头信息
	req_->request_header().set_method(method)
		.set_range(range_from, range_to);

	// 发送 HTTP 请求数据
	if (req_->request(NULL, 0) == false)
	{
		logger_error("send request error, url: %s", url_);
		return false;
	}

	// 获得文件长度
	acl_int64 length = req_->get_range_max();
	if (length <= 0)
	{
		// 可能是服务器不支持 range 功能,所以还得从头下载
		return false;
	}

	http_client* conn = req_->get_client();
	if (conn == NULL)
		logger_fatal("no connect to server");

	// 回调子类虚接口实现
	if (on_response(conn) == false)
	{
		logger_error("deny url(%s)'s download", url_);
		return false;
	}

	// 回调子类虚接口实现
	if (on_length(length) == false)
	{
		logger_error("deny url(%s)'s download", url_);
		return false;
	}

	return save(req_);
}
示例#2
0
/*  Called when all the headers are complete but before
    the content body, if present.
    Returning 1 from here tells the joyent parser
    that the message has no body (e.g. a HEAD request).
*/
int
basic_parser::do_headers_complete()
{
    check_header();
    auto const p (reinterpret_cast <joyent::http_parser const*> (&state_));
    bool const keep_alive (joyent::http_should_keep_alive (p) != 0);
    if (p->type == joyent::http_parser_type::HTTP_REQUEST)
        return on_request (joyent::convert_http_method (
            joyent::http_method(p->method)), url_,
                p->http_major, p->http_minor, keep_alive, p->upgrade) ? 0 : 1;
    return on_response (p->status_code, status_,
        p->http_major, p->http_minor, keep_alive, p->upgrade) ? 0 : 1;
}
示例#3
0
  /**
   * @brief      Creates a HTTP Response handler by allocating a client connector
   *             with restricted lifetime to the response handler itself.
   *
   * @param[in]  cb    A connect callback
   * @param[in]  key   The WS key
   *
   * @return     Returns a Response handler with a captured client connector.
   */
  static Response_handler create_response_handler(ConnectCallback cb, std::string key)
  {
    // @todo Try replace with unique_ptr
    // create a new instance of a client connector
    //auto ptr = std::unique_ptr<WS_client_connector>{
    //  new WS_client_connector(std::move(cb), std::move(key))
    //};
    auto ptr = std::make_shared<WS_client_connector>(std::move(cb), std::move(key));

    return [ ptr{std::move(ptr)} ]
           (auto err, auto res, auto& conn)
           {
            ptr->on_response(err, std::move(res), conn);
           };
  }
示例#4
0
/*---------------------------------------------------------------------------*/
static int on_message(struct xio_session *session, struct xio_msg *msg,
		      int more_in_batch, void *cb_prv_data)
{
	switch (msg->type) {
	case XIO_MSG_TYPE_REQ:
		on_request(session, msg, more_in_batch, cb_prv_data);
		break;
	case XIO_MSG_TYPE_RSP:
		on_response(session, msg, more_in_batch, cb_prv_data);
		break;
	default:
		printf("unknown message type : %d\n", msg->type);
		break;
	}

	return 0;
}
示例#5
0
bool http_download::save_total(const char* body, size_t len)
{
	// 发送不带 range 字段的下载请求

	http_method_t method = body && len > 0
		? HTTP_METHOD_POST : HTTP_METHOD_GET;

	// 设置 HTTP 请求头信息
	req_->request_header().set_method(method);

	// 发送 HTTP 请求数据
	if (req_->request(body, len) == false)
	{
		logger_error("send request error, url: %s", url_);
		return false;
	}

	http_client* conn = req_->get_client();
	if (conn == NULL)
		logger_fatal("no connect to server");

	// 回调子类虚接口实现
	if (on_response(conn) == false)
	{
		logger_error("deny url(%s)'s download", url_);
		return false;
	}

	// 获得文件长度
	acl_int64 length = conn->body_length();

	// 回调子类虚接口实现
	if (on_length(length) == false)
	{
		logger_error("deny url(%s)'s download", url_);
		return false;
	}

	// 开始下载数据体过程
	return save(req_);
}
示例#6
0
void client::handle_read_content( const boost::system::error_code& error )
{
    if ( ! error )
    {
      std::istream stream( &reply_ );
      std::string content;

      while ( std::getline( stream, content ) )
          response_->set_content( content );

      // Continue reading remaining data until EOF.
      boost::asio::async_read( socket_, 
                               reply_,
                               boost::asio::transfer_at_least( 1 ),
                               boost::bind( &client::handle_read_content, 
                                            this,
                                            boost::asio::placeholders::error ) );
    }
    else if ( error != boost::asio::error::eof ) // expected, for short-lived http connections
        on_error( error );

    on_response( response_ );
}
示例#7
0
文件: HTTP.cpp 项目: josemarone/Cosa
int
HTTP::Client::get(const char* url, uint32_t ms)
{
  if (m_sock == NULL) return (-1);
  const uint8_t PREFIX_MAX = 7;
  uint16_t port = 80;
  char hostname[HOSTNAME_MAX];
  uint32_t start;
  uint8_t i;
  int res;
  char c;

  // Parse given url for hostname
  if (memcmp_P(url, PSTR("http://"), PREFIX_MAX) == 0) url += PREFIX_MAX;
  i = 0; 
  while (1) {
    c = *url;
    hostname[i] = c;
    if (c == 0) break;
    url += 1;
    if ((c == '/') || (c == ':')) break;
    i += 1;
    if (i == sizeof(hostname)) return (-2);
  }
  if (c != 0) hostname[i] = 0;

  // Parse url for port number
  if (c == ':') {
    char num[16];
    i = 0;
    while (isdigit(c = *url)) {
      url += 1;
      num[i++] = c;
      if (i == sizeof(num)) return (-2);
    }
    if (i == 0) return (-2);
    num[i] = 0;
    port = atoi(num);
    if (c == '/') url += 1;
  }

  // Connect to the server
  res = m_sock->connect((const char*) hostname, port);
  if (res != 0) goto error;
  while ((res = m_sock->isconnected()) == 0) Watchdog::delay(16);
  if (res == 0) res = -3;
  if (res < 0) goto error;
  
  // Send a HTTP request
  m_sock->puts_P(PSTR("GET /"));
  m_sock->puts(url);
  m_sock->puts_P(PSTR(" HTTP/1.1" CRLF "Host: "));
  m_sock->puts(hostname);
  m_sock->puts_P(PSTR(CRLF "Connection: close" CRLF CRLF));
  m_sock->flush();
  
  // Wait for the response
  start = Watchdog::millis();
  while (((res = m_sock->available()) == 0) &&
	 ((ms == 0L) || (Watchdog::millis() - start < ms)))
    Watchdog::delay(16);
  if (res == 0) res = -4;
  if (res < 0) goto error;
  on_response(hostname, url);
  res = 0;

  // Close the connect and reopen for additional get
 error:
  m_sock->disconnect();
  m_sock->close();
  m_sock->open(Socket::TCP, 0, 0);
  return (res);
}
示例#8
0
文件: task.cpp 项目: SunnyGyb/rDSN
void rpc_response_task::exec() 
{ 
    on_response(error(), _request, _response);
}
示例#9
0
void SelectInstance::on_row(const Gtk::TreePath& path, Gtk::TreeViewColumn* column) {
	on_response(1);
}
bool CDistributedObjectBase::do_response(const TDistributedMessage* message)
{
    // callback
    return on_response(message);
}