Пример #1
0
ret_t
cherokee_handler_tmi_init (cherokee_handler_tmi_t *hdl)
{
    ret_t ret;
    cherokee_connection_t *conn = HANDLER_CONN(hdl);
    cherokee_buffer_t	 *tmp  = &HANDLER_THREAD(hdl)->tmp_buf1;
    cherokee_handler_tmi_props_t *props = HANDLER_TMI_PROPS(hdl);

    /* We are going to look for gzipped encoding */
    cherokee_buffer_clean (tmp);
    ret = cherokee_header_copy_known (&conn->header, header_content_encoding, tmp);
    if (ret == ret_ok && cherokee_buffer_cmp_str(tmp, "gzip") == 0) {
        TRACE(ENTRIES, "ZeroMQ: incomming header '%s'\n", tmp->buf);
        hdl->inflated = true;
    } else {
        cherokee_buffer_clean (tmp);
        ret = cherokee_header_copy_known (&conn->header, header_content_type, tmp);
        if (ret == ret_ok && (cherokee_buffer_cmp_str(tmp, "application/gzip") == 0 || cherokee_buffer_cmp_str(tmp, "application/zip") == 0)) {
            TRACE(ENTRIES, "ZeroMQ: incomming header '%s'\n", tmp->buf);
            hdl->inflated = true;
        } else {
            hdl->inflated = false;
        }
    }

#ifdef LIBXML_PUSH_ENABLED
    if (props->validate_xml) {
        hdl->validate_xml = true;
        hdl->ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
        xmlCtxtUseOptions(hdl->ctxt, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET | XML_PARSE_COMPACT);

        if (hdl->inflated) {
            /* allocate inflate state */
            hdl->strm.zalloc = Z_NULL;
            hdl->strm.zfree = Z_NULL;
            hdl->strm.opaque = Z_NULL;
            hdl->strm.avail_in = 0;
            hdl->strm.next_in = Z_NULL;
            hdl->z_ret = inflateInit2(&(hdl->strm), 16+MAX_WBITS);
            if (hdl->z_ret != Z_OK)
                hdl->validate_xml = false;
        }
    }
#endif

    if (!hdl->inflated) {
        /* If we end up here that means content is plain, lets set up an encoder */
        ret = props->encoder_props->instance_func((void **)&hdl->encoder, props->encoder_props);
        if (unlikely (ret != ret_ok)) {
            return ret_error;
        }

        ret = cherokee_encoder_init (hdl->encoder, conn);
        if (unlikely (ret != ret_ok)) {
            return ret_error;
        }
    }

    return ret_ok;
}
Пример #2
0
ret_t
cherokee_handler_zeromq_init (cherokee_handler_zeromq_t *hdl)
{
	ret_t							 ret;
	cherokee_buffer_t				*tmp   = &HANDLER_THREAD(hdl)->tmp_buf1;
	cherokee_connection_t			*conn  = HANDLER_CONN(hdl);
	cherokee_handler_zeromq_props_t *props = HANDLER_ZEROMQ_PROPS(hdl);

	/* We are going to look for gzipped encoding */
	cherokee_buffer_clean (tmp);
	ret = cherokee_header_copy_known (&conn->header, header_content_encoding, tmp);
	if (ret == ret_ok && cherokee_buffer_cmp_str(tmp, "gzip") == 0) {
		TRACE(ENTRIES, "ZeroMQ: incomming header '%s'\n", tmp->buf);
		return ret_ok;
	} else {
		cherokee_buffer_clean (tmp);
		ret = cherokee_header_copy_known (&conn->header, header_content_type, tmp);
		if (ret == ret_ok && cherokee_buffer_cmp_str(tmp, "application/gzip") == 0) {
			TRACE(ENTRIES, "ZeroMQ: incomming header '%s'\n", tmp->buf);
			return ret_ok;
		}
	}

	/* If we end up here that means content is plain, lets set up an encoder */
	ret = props->encoder_props->instance_func((void **)&hdl->encoder, props->encoder_props);
	if (unlikely (ret != ret_ok)) {
		return ret_error;
	}

	ret = cherokee_encoder_init (hdl->encoder, conn);
	/* TODO: this is a great idea for KV78turbo, but being able to configure
	 * the reply (KV6, 15, 17) sounds like a good idea too.
	 */
	conn->error_code = http_no_content;
	return ret_error;
}
Пример #3
0
static ret_t
build_log_string (cherokee_logger_ncsa_t *logger,
		  cherokee_connection_t  *cnt,
		  cherokee_buffer_t      *buf)
{
	ret_t              ret;
	const char        *method;
	const char        *username;
	const char        *version;
	cuint_t            method_len                   = 0;
	size_t             username_len                 = 0;
	cuint_t            version_len                  = 0;
	cherokee_buffer_t *referer                      = &logger->referer;
	cherokee_buffer_t *useragent                    = &logger->useragent;
	char               ipaddr[CHE_INET_ADDRSTRLEN];

	/* Look for the user
	 */
	if (cnt->validator && !cherokee_buffer_is_empty (&cnt->validator->user)) {
		username_len = cnt->validator->user.len;
		username = cnt->validator->user.buf;
	} else {
		username_len = 1;
		username = "******";
	}

	/* Get the method and version strings
	 */
	ret = cherokee_http_method_to_string (cnt->header.method, &method, &method_len);
	if (unlikely (ret < ret_ok)) {
		method     = "";
		method_len = 0;
	}

	ret = cherokee_http_version_to_string (cnt->header.version, &version, &version_len);
	if (unlikely (ret < ret_ok)) {
		version     = "";
		version_len = 0;
	}

	/* Build the log string
	 *
	 * "%s - %s [%02d/%s/%d:%02d:%02d:%02d %c%02d%02d] \"%s %s %s\" %d "
	 * FMT_OFFSET
	 */
	if (cherokee_buffer_is_empty (&cnt->logger_real_ip)) {
		memset (ipaddr, 0, sizeof(ipaddr));
		cherokee_socket_ntop (&cnt->socket, ipaddr, sizeof(ipaddr)-1);
		cherokee_buffer_add (buf, ipaddr, strlen(ipaddr));
	} else {
		cherokee_buffer_add_buffer (buf, &cnt->logger_real_ip);
	}

	cherokee_buffer_add_str (buf, " - ");
	cherokee_buffer_add     (buf, username, username_len);

	/* " [date time] "
	 */
	cherokee_buffer_add_buffer (buf, &now);
	cherokee_buffer_add        (buf, method, method_len);
	cherokee_buffer_add_char   (buf, ' ');

	if (! cherokee_buffer_is_empty (&cnt->request_original)) {
		cherokee_buffer_add_buffer (buf, &cnt->request_original);
		if (! cherokee_buffer_is_empty (&cnt->query_string_original)) {
			cherokee_buffer_add_char   (buf, '?');
			cherokee_buffer_add_buffer (buf, &cnt->query_string_original);
		}
	} else {
		cherokee_buffer_add_buffer (buf, &cnt->request);
		if (! cherokee_buffer_is_empty (&cnt->query_string)) {
			cherokee_buffer_add_char   (buf, '?');
			cherokee_buffer_add_buffer (buf, &cnt->query_string);
		}
	}

	cherokee_buffer_add_char   (buf, ' ');
	cherokee_buffer_add        (buf, version, version_len);
	cherokee_buffer_add_str    (buf, "\" ");

	if (unlikely (cnt->error_internal_code != http_unset)) {
		cherokee_buffer_add_long10 (buf, cnt->error_internal_code);
	} else {
		cherokee_buffer_add_long10 (buf, cnt->error_code);
	}

	cherokee_buffer_add_char (buf, ' ');
	cherokee_buffer_add_ullong10 (buf, cnt->tx);

	/* Look for the "combined" information
	 */
	if (!logger->combined) {
		cherokee_buffer_add_char (buf, '\n');
		return ret_ok;
	}

	/* "combined" information
	 */
	cherokee_buffer_clean (referer);
	cherokee_buffer_clean (useragent);

	cherokee_header_copy_known (&cnt->header, header_referer, referer);
	cherokee_header_copy_known (&cnt->header, header_user_agent, useragent);
	cherokee_buffer_ensure_addlen (buf, 8 + referer->len + referer->len);

	if (referer->len > 0) {
		cherokee_buffer_add_str    (buf, " \"");
		cherokee_buffer_add_buffer (buf, referer);
		cherokee_buffer_add_str    (buf, "\" \"");
	} else {
		cherokee_buffer_add_str (buf, " \"-\" \"");
	}

	if (useragent->len > 0) {
		cherokee_buffer_add_buffer (buf, useragent);
	}

	cherokee_buffer_add_str (buf, "\"\n");
	return ret_ok;
}
Пример #4
0
static ret_t
downloader_header_read (cherokee_downloader_t *downloader,
                        cherokee_buffer_t     *tmp1,
                        cherokee_buffer_t     *tmp2)
{
	ret_t               ret;
	cuint_t             len;
	size_t              read_      = 0;
	cherokee_socket_t  *sock       = &downloader->socket;
	cherokee_http_t     error_code = http_bad_request;

	UNUSED(tmp2);

	ret = cherokee_socket_bufread (sock, &downloader->reply_header, DEFAULT_RECV_SIZE, &read_);
	switch (ret) {
	case ret_eof:
		return ret_eof;

	case ret_eagain:
		return ret_eagain;

	case ret_ok:
		/* Count
		 */
		downloader->info.headers_recv += read_;

		/* Check the header. Is it complete?
		 */
		ret = cherokee_header_has_header (downloader->header, &downloader->reply_header, 0);
		switch (ret) {
		case ret_ok:
			break;
		case ret_not_found:
			/* It needs to read more headers ...
			 */
			return ret_eagain;
		default:
			/* Too many initial CRLF
			 */
			return ret_error;
		}

		/* Parse the header
		 */
		ret = cherokee_header_parse (downloader->header,
		                             &downloader->reply_header,
		                             &error_code);
		if (unlikely (ret != ret_ok)) return ret_error;

		/* Look for the length, it will need to drop out the header from the buffer
		 */
		cherokee_header_get_length (downloader->header, &len);

		/* Maybe it has some body
		 */
		if (downloader->reply_header.len > len) {
			uint32_t body_chunk;

			/* Skip the CRLF separator and copy the body
			 */
			len += 2;
			body_chunk = downloader->reply_header.len - len;

			downloader->info.body_recv += body_chunk;
			cherokee_buffer_add (&downloader->body, downloader->reply_header.buf + len, body_chunk);

			cherokee_buffer_drop_ending (&downloader->reply_header, body_chunk);
		}

		/* Try to read the "Content-Length" response header
		 */
		ret = cherokee_header_has_known (downloader->header, header_content_length);
		if (ret == ret_ok) {
			cherokee_buffer_clean (tmp1);
			ret = cherokee_header_copy_known (downloader->header, header_content_length, tmp1);
			if (ret == ret_ok) {
				ret = cherokee_atou (tmp1->buf, &downloader->content_length);
#ifdef TRACE_ENABLED
				if (ret == ret_ok) {
					TRACE (ENTRIES, "Known length: %d bytes\n", downloader->content_length);
				} else {
					TRACE (ENTRIES, "Could not parse Content-Length\n");
				}
#endif
			}
		}

		return ret_ok;

	case ret_error:
		/* Opsss.. something has failed
		 */
		return ret_error;

	default:
		RET_UNKNOWN (ret);
		return ret;
	}

	return ret_error;
}