get.c 3.24 KB
Newer Older
1 2
/*
	belle-sip - SIP (RFC3261) library.
Simon Morlat's avatar
Simon Morlat committed
3
    Copyright (C) 2013  Belledonne Communications SARL
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "belle-sip/belle-sip.h"

static belle_sip_stack_t *stack=NULL;

static void process_response(void *data, const belle_http_response_event_t *event){
	belle_http_response_t *resp=event->response;
	const char *body=belle_sip_message_get_body(BELLE_SIP_MESSAGE(resp));
35 36
	fprintf(stdout,"Got response:\n");

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	if (body){
		fprintf(stdout,"%s",body);
	}
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
}

static void process_io_error(void *data, const belle_sip_io_error_event_t *event){
	fprintf(stderr,"IO error\n");
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
}

static void process_timeout(void *data, const belle_sip_timeout_event_t *event){
	fprintf(stderr,"Timeout\n");
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
}

Simon Morlat's avatar
Simon Morlat committed
53 54 55 56 57
static void usage(const char *argv0){
	fprintf(stderr,"Usage:\n%s <http uri> [--ca-path <path> ] [--debug] [--no-tls-check]\n",argv0);
	exit(-1);
}

58 59 60 61 62
int main(int argc, char *argv[]){
	belle_http_provider_t *prov;
	belle_http_request_t *req;
	belle_generic_uri_t *uri;
	belle_http_request_listener_callbacks_t cbs={0};
63 64 65
	const char *ca_path = NULL;
	belle_tls_crypto_config_t *cfg;
	int i;
Simon Morlat's avatar
Simon Morlat committed
66
	int check_tls = 1;
67 68
	
	if (argc<2){
Simon Morlat's avatar
Simon Morlat committed
69
		usage(argv[0]);
70
	}
71
	bctbx_init_logger(1);
72 73 74 75 76
	for (i = 2; i < argc; ++i){
		if (strcmp(argv[i], "--ca-path") == 0){
			i++;
			ca_path = argv[i];
		}else if (strcmp(argv[i], "--debug")==0){
77 78
			fprintf(stderr, "Logs are enabled.\n");
			bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_DEBUG);
Simon Morlat's avatar
Simon Morlat committed
79 80 81 82
		}else if (strcmp(argv[i], "--no-tls-check")==0){
			check_tls = 0;
		}else{
			usage(argv[0]);
83 84 85
		}
	}
	stack=belle_sip_stack_new(NULL);
86
	prov=belle_sip_stack_create_http_provider(stack,"::0");
Simon Morlat's avatar
Simon Morlat committed
87
	cfg = belle_tls_crypto_config_new();
88
	if (ca_path){
Simon Morlat's avatar
Simon Morlat committed
89
		
90 91
		belle_tls_crypto_config_set_root_ca(cfg, ca_path);
	}
Simon Morlat's avatar
Simon Morlat committed
92 93
	if (!check_tls) belle_tls_crypto_config_set_verify_exceptions(cfg, BELLE_TLS_VERIFY_ANY_REASON);
	belle_http_provider_set_tls_crypto_config(prov, cfg);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	uri=belle_generic_uri_parse(argv[1]);
	if (!uri){
		fprintf(stderr,"Bad uri %s\n",argv[1]);
		return -1;
	}
	cbs.process_response=process_response;
	cbs.process_io_error=process_io_error;
	cbs.process_timeout=process_timeout;
	req=belle_http_request_create("GET",uri,NULL);
	belle_http_provider_send_request(prov,req,belle_http_request_listener_create_from_callbacks(&cbs,NULL));
	belle_sip_stack_main(stack);
	belle_sip_object_unref(prov);
	belle_sip_object_unref(stack);
	return 0;
}