Beispiel #1
0
bool
Server::StartConnection(void)
{
    xbee_err ret;

    if (_connection != nullptr)
        EndConnection();
    if ((ret = xbee_conNew(_xbee, &_connection, "Local AT", NULL)) != XBEE_ENONE) {
        printf("xbee_conNew: %d (%s)\n", ret, xbee_errorToStr(ret));
        return false;
    }
    return true;
}
//-------------------------------------------------------------------------
// Main
// Handle the Request, Handle (Send) Response), End the Connection
//-------------------------------------------------------------------------
void CWebserverConnection::HandleConnection() {
	gettimeofday(&tv_connection_start, &tz_connection_start);

	// get the request
	if (Request.HandleRequest()) {
		// determine time from Connection creation until now
		gettimeofday(&tv_connection_Response_start,
				&tz_connection_Response_start);
		enlapsed_request = ((tv_connection_Response_start.tv_sec
				- tv_connection_start.tv_sec) * 1000000
				+ (tv_connection_Response_start.tv_usec
						- tv_connection_start.tv_usec));

		// Keep-Alive checking
#ifdef Y_CONFIG_FEATURE_KEEP_ALIVE
		if(string_tolower(Request.HeaderList["Connection"]) == "close"
				|| (httprotocol != "HTTP/1.1" && string_tolower(Request.HeaderList["Connection"]) != "keep-alive")
				|| !Webserver->CheckKeepAliveAllowedByIP(sock->get_client_ip()))
		keep_alive = false;
#else
		keep_alive = false;
#endif
		// Send a response
		Response.SendResponse();

		// determine time for SendResponse
		gettimeofday(&tv_connection_Response_end, &tz_connection_Response_end);
		enlapsed_response = ((tv_connection_Response_end.tv_sec
				- tv_connection_Response_start.tv_sec) * 1000000
				+ (tv_connection_Response_end.tv_usec
						- tv_connection_Response_start.tv_usec));

		// print production times			
		log_level_printf(1, "enlapsed time request:%ld response:%ld url:%s\n",
				enlapsed_request, enlapsed_response,
				(Request.UrlData["fullurl"]).c_str());

	} else {
		RequestCanceled = true;
		keep_alive = false; // close this connection socket
		//		dperror("Error while parsing request\n");
		log_level_printf(1, "request canceled: %s\n", strerror(errno));
	}
	EndConnection();
}
void HttpClientSocket::OnData(const char *buf,size_t len)
{
	if (m_fil)
	{
		m_fil -> fwrite(buf, 1, len);
	}
	if (m_data_ptr)
	{
		size_t left = m_data_size - m_content_ptr;
		size_t sz = len < left ? len : left;
		if (sz > 0)
			memcpy(m_data_ptr + m_content_ptr, buf, sz);
		m_content_ptr += sz;
		if (len > left)
		{
			Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR);
		}
	}
	if (m_content_ptr == m_content_length && m_content_length)
	{
		EndConnection();
	}
}
void HttpClientSocket::OnHeaderComplete()
{
	if (m_filename.size())
	{
		m_fil = new File;
		if (!m_fil -> fopen(m_filename, "wb"))
		{
			delete m_fil;
			m_fil = NULL;
		}
	}
	if (!m_data_ptr && m_content_length > 0)
	{
		m_data_ptr = new unsigned char[m_content_length];
		m_data_size = m_content_length;
	}
	// make sure we finish in a good way even when response
	// has content-length: 0
	if (m_content_length_is_set && m_content_length == 0)
	{
		EndConnection();
	}
}