get.c 2.85 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2
/*
	belle-sip - SIP (RFC3261) library.
Simon Morlat's avatar
Simon Morlat committed
3
    Copyright (C) 2013  Belledonne Communications SARL
Simon Morlat's avatar
Simon Morlat committed
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    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));
	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));
}

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};
56 57 58
	const char *ca_path = NULL;
	belle_tls_crypto_config_t *cfg;
	int i;
Simon Morlat's avatar
Simon Morlat committed
59 60
	
	if (argc<2){
61
		fprintf(stderr,"Usage:\n%s <http uri> [--ca-path <path> ] [--debug]\n",argv[0]);
Simon Morlat's avatar
Simon Morlat committed
62 63
		return -1;
	}
64 65 66 67 68
	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){
Simon Morlat's avatar
Simon Morlat committed
69 70 71 72
			belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
		}
	}
	stack=belle_sip_stack_new(NULL);
73 74 75 76 77 78
	prov=belle_sip_stack_create_http_provider(stack,"::0");
	if (ca_path){
		cfg = belle_tls_crypto_config_new();
		belle_tls_crypto_config_set_root_ca(cfg, ca_path);
		belle_http_provider_set_tls_crypto_config(prov, cfg);
	}
Simon Morlat's avatar
Simon Morlat committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	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;
}