Esempio n. 1
0
cp_string *cp_db_connection_unescape_binary(cp_db_connection *connection,
											char *src)
{
	if (connection->data_source->act->unescape_binary)
		return (*connection->data_source->act->unescape_binary)(connection, src);

	cp_error(CP_METHOD_NOT_IMPLEMENTED, 
			"%s driver does not implement unescape_binary", 
			connection->data_source->act->dbms_lit);


	return cp_string_create(src, strlen(src));
}
Esempio n. 2
0
int file_service(cp_http_request *request, cp_http_response *response)
{
    int rc = 0;
    char *ext;
    char path[PATHLEN];
    int uri_len;
    char buf[FBUFSIZE];
    FILE *fp;
    cp_string *body = NULL;

#ifdef DEBUG
    cp_http_request_dump(request);
#endif

    ext = strrchr(request->uri, '.');
    if (ext) 
        cp_http_response_set_content_type_string(response, 
                                                 cp_hashtable_get(mimemap, ++ext));

    /* check len, avoid buffer overrun */
    uri_len = strlen(request->uri);
    if (uri_len + strlen(document_root) >= PATHLEN)
    {
        cp_http_response_set_content_type(response, HTML);
        cp_http_response_set_status(response, HTTP_404_NOT_FOUND);
        response->body = strdup(HTTP404_PAGE);
        return HTTP_CONNECTION_POLICY_CLOSE;
    }
        
#ifdef CP_HAS_SNPRINTF
    snprintf(path, PATHLEN, "%s%s", document_root, request->uri);
#else
    sprintf(path, "%s%s", document_root, request->uri);
#endif /* CP_HAS_SNPRINTF */
    if (path[strlen(path) - 1] == '/') 
    {
        strlcat(path, "index.html", PATHLEN);
        response->content_type = HTML;
    }

    fp = fopen(path, "rb");
    if (fp == NULL)
    {
        cp_http_response_set_content_type(response, HTML);
        cp_http_response_set_status(response, HTTP_404_NOT_FOUND);
#ifdef CP_HAS_SNPRINTF
        snprintf(buf, FBUFSIZE, HTTP404_PAGE_uri, request->uri);
#else
        sprintf(buf, HTTP404_PAGE_uri, request->uri);
#endif /* CP_HAS_SNPRINTF */
        response->body = strdup(buf);
        return HTTP_CONNECTION_POLICY_CLOSE;
    }

#ifdef __TRACE__
    DEBUGMSG("retrieving [%s]", path);
#endif
    while ((rc = fread(buf, 1, FBUFSIZE, fp)) > 0)
    {
        if (body == NULL)
            body = cp_string_create(buf, rc);
        else
            cp_string_cat_bin(body, buf, rc);
    }
    fclose(fp);
    
    cp_http_response_set_status(response, HTTP_200_OK);

    response->content = body;

    return HTTP_CONNECTION_POLICY_KEEP_ALIVE;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
	cp_client *client;
    struct sigaction act;
	char request[0x100];
	char *hostbuf, *host, *uri, *pbuf;
	int port;
	cp_string *buf;
	char *CA_file;
	int use_ssl = 0;
	int rc;

	act.sa_handler = sigpipe_handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;

	sigaction(SIGPIPE, &act, NULL);

	cp_log_init("test_client.log", LOG_LEVEL_DEBUG);

	if (argc < 2) 
	{
		printf("please specify url\n");
		exit(1);
	}

	if (argc < 3)
	{
		printf("no CA file specified - SSL disabled\n");
		CA_file = NULL;
	}
	else
	{
		use_ssl = 1;
		CA_file = argv[2];
		cp_client_ssl_init();
	}

	hostbuf = strdup(argv[1]);
	if ((host = strstr(hostbuf, "://")) != NULL)
		host += 3;
	else
		host = hostbuf;

	pbuf = strchr(host, ':');
	if (pbuf)
	{
		*pbuf = '\0';
		port = atoi(++pbuf);
	}
	else
	{
		port = 443;
		pbuf = host;
	}

	uri = strchr(pbuf, '/');
	if (uri == NULL) 
		uri = strdup(NOURI);
	else
	{
		char *tmp = uri;
		uri = strdup(tmp);
		*tmp = '\0';
	}

	if (CA_file)
		client = cp_client_create_ssl(host, port, CA_file, NULL, SSL_VERIFY_NONE);
	else
		client = cp_client_create(host, port);
	if (client == NULL)
	{
		printf("can\'t create client\n");
		exit(2);
	}

	if ((rc = cp_client_connect(client)) == -1)
	{
		printf("connect failed\n");
		exit(3);
	}
	else if (rc > 0)
	{
		printf("verification error: %s\n", ssl_verification_error_str(rc));
	}

	if (client->server_certificate)
		cp_client_verify_hostname(client);

	sprintf(request, request_fmt, uri, host);

	if ((rc = cp_client_write(client, request, strlen(request))) == -1)
		perror("write");

	buf = cp_string_create("", 0);
	rc = cp_client_read_string(client, buf, 0);

	printf("%s\n[%d bytes read]\n", cp_string_tocstr(buf), cp_string_len(buf));

	free(uri);
	free(hostbuf);
	cp_string_destroy(buf);
	cp_client_close(client);
	cp_client_destroy(client);

	if (use_ssl)
		cp_client_ssl_shutdown();

	cp_log_close();

	return 0;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
	int rcc;
	cp_httpclient *client;
	regex_t url_re;
	regmatch_t rm[6];
	
	cp_string *host;
	int ssl;
	int port;
	cp_string *uri;
	
	cp_httpclient_result *result;
	cp_http_response *res;

	if (argc < 2)
	{
		printf("do this: %s \"url\"\n", argv[0]);
		return 1;
	}

	if ((rcc = regcomp(&url_re, url_re_lit, REG_EXTENDED | REG_ICASE)) != 0)
	{
		char errbuf[0x100];
		regerror(rcc, &url_re, errbuf, 0x100);
		printf("regex error: %s\n", errbuf);
		return 2;
	}
	
	if (regexec(&url_re, argv[1], 6, rm, 0))
	{
		printf("can\'t parse url: %s\n", argv[1]);
		return 3;
	}
	
	ssl = rm[2].rm_so != -1;
	host = cp_string_create(&argv[1][rm[3].rm_so], rm[3].rm_eo - rm[3].rm_so);
	if (rm[4].rm_so != -1)
		port = atoi(&argv[1][rm[4].rm_so + 1]);
	else
		port = ssl ? 443 : 80;
	if (rm[5].rm_so != -1)
		uri = cp_string_create(&argv[1][rm[5].rm_so], rm[5].rm_eo - rm[5].rm_so);
	else
		uri = cp_string_create("/", 1);

	printf("host: %s\n", cp_string_tocstr(host));
	printf("port: %d\n", port);
	printf("uri:  %s\n", cp_string_tocstr(uri));
#ifdef CP_USE_SSL
	printf("ssl:  %s\n", ssl ? "yes" : "no");
#else
	if (ssl) 
	{
		fprintf(stderr, "can\'t fetch: cprops configured with --disable-ssl\n");
		exit(1);
	}
#endif /* CP_USE_SSL */
	
	cp_log_init("test_httpclient.log", 0); //LOG_LEVEL_DEBUG);
	cp_httpclient_init();
	
#ifdef CP_USE_SSL
	if (ssl)
		client = cp_httpclient_create_ssl(cp_string_tocstr(host), port, "../test/cacert.pem", NULL, SSL_VERIFY_NONE);
	else
#endif /* CP_USE_SSL */
		client = cp_httpclient_create(cp_string_tocstr(host), port);

	if (client)
	{
		result = cp_httpclient_fetch(client, cp_string_tocstr(uri));
		if (result)
		{
			res = cp_httpclient_result_get_response(result);
			if (res && res->content) 
			{
				struct timeval done;
				unsigned long span;
				
				gettimeofday(&done, NULL);
				span = done.tv_sec * 1000000 + done.tv_usec - 
						(client->socket->created.tv_sec * 1000000 + 
						 client->socket->created.tv_usec);
				
				printf("%s\n", cp_string_tocstr(res->content));
				printf("%ld bytes read (content: %ld bytes) in %ld.%03ld sec (%03.3f kb/sec)\n",
						client->socket->bytes_read, cp_string_len(res->content), 
						(span / 1000000), (span % 1000000) / 1000,
						1000.0 * (float) client->socket->bytes_read / (float) span);
				cp_httpclient_result_destroy(result);
			}
		}
		cp_httpclient_destroy(client);
	}
	else
		printf("can\'t make connection - cp_httpclient_create failed\n");

	cp_string_destroy(host);
	cp_string_destroy(uri);

	cp_httpclient_shutdown();
	cp_log_close();

	regfree(&url_re);
	return 0;
}
Esempio n. 5
0
invoke_spec *parse_prm(char *prm)
{
	cp_httpclient *client;
	regmatch_t rm[6];
	invoke_spec *spec = NULL;
	
	cp_string *host;
	int ssl;
	int port;
	cp_string *uri;

	if (regexec(&url_re, prm, 6, rm, 0))
	{
		printf("can\'t parse url: %s\n", prm);
		return NULL;
	}
	
	ssl = rm[2].rm_so != -1;
	host = cp_string_create(&prm[rm[3].rm_so], rm[3].rm_eo - rm[3].rm_so);
	if (rm[4].rm_so != -1)
		port = atoi(&prm[rm[4].rm_so + 1]);
	else
		port = ssl ? 443 : 80;
	if (rm[5].rm_so != -1)
		uri = cp_string_create(&prm[rm[5].rm_so], rm[5].rm_eo - rm[5].rm_so);
	else
		uri = cp_string_create("/", 1);

	printf("host: %s\n", cp_string_tocstr(host));
	printf("port: %d\n", port);
	printf("uri:  %s\n", cp_string_tocstr(uri));

#ifdef CP_USE_SSL
    printf("ssl:  %s\n", ssl ? "yes" : "no");
#else
    if (ssl)
    {
        fprintf(stderr, "can\'t fetch: cprops configured with --disable-ssl\n");
        exit(1);
    }
#endif /* CP_USE_SSL */
	
#ifdef CP_USE_SSL
	if (ssl)
		client = cp_httpclient_create_ssl(cp_string_tocstr(host), port, "../test/cacert.pem", "test", SSL_VERIFY_NONE);
	else
#endif /* CP_USE_SSL */
		client = cp_httpclient_create(cp_string_tocstr(host), port);
	
	cp_string_destroy(host);

	if (client == NULL)
	{
		free(uri);
		return NULL;
	}
	
	spec = calloc(1, sizeof(invoke_spec));
	spec->client = client;
	spec->uri = uri->data;
	cp_string_drop_wrap(uri);

	return spec;
}