http-client.c 9.5 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@file http-client.c  Simple HTTP tool.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Created: Fri Mar 30 12:05:21 2001 ppessi
 */

#include "config.h"

Pekka Pessi's avatar
Pekka Pessi committed
34
/**@page http_client Make HTTP request
Pekka Pessi's avatar
Pekka Pessi committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
 * 
 * @par Name
 * http-client - HTTP request tool
 *
 * @par Synopsis
 *
 * <tt>http-client [OPTIONS] url</tt>
 *
 * @par Description
 * 
 * The @em http-client utility sends a HTTP request to an HTTP server or proxy.
 *
 * @par 
 *
 * The @em http-client tool will print out status line and interesting
 * headers from the response. The message body is also printed.
 *
 * @par Options
 *
 * The @e http-client utility accepts following command line options:
 * <dl>
 * <dt>--method=name</dt>
 * <dd>Specify the request method name (GET by default).
 * </dd>
 * <dt>--proxy=url</dt>
 * <dd>Specifies the proxy via which the request will be sent.
 * </dd>
 * <dt>--ua=value</dt>
 * <dd>Specifies the User-Agent header field.
 * </dd>
 * <dt>--mf=n</dt>
 * <dd>Specify the initial Max-Forwards count.
 * </dd>
 * <dt>--pipe</dt>
 * <dd>Use pipelining (do not shutdown client connection after request).
 * </dd>
 * <dt>--extra</dt>
 * <dd>Insert standard input to the requests.
 * </dd>
 * </dl>
 *
 * @par Examples
 *
 * You want to query supported features of http://connecting.nokia.com:
 * @code
 * $ http-client --method OPTIONS http://connecting.nokia.com
 * @endcode
 *
 * @par Environment
 * @c NTH_DEBUG, @c TPORT_DEBUG, @c TPORT_LOG.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * <hr>
 */

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

typedef struct context_s context_t;
#define NTH_CLIENT_MAGIC_T context_t

100 101 102 103 104
#include <sofia-sip/nth.h>
#include <sofia-sip/http_header.h>
#include <sofia-sip/http_tag.h>
#include <sofia-sip/tport_tag.h>
#include <sofia-sip/auth_client.h>
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

struct context_s {
  su_home_t   	  c_home[1];
  su_root_t   	 *c_root;
  nth_engine_t 	 *c_engine;
  nth_client_t   *c_clnt;
  char const     *c_user;
  char const     *c_pass;
  auth_client_t  *c_auth;
  int             c_pre;
  int             c_pending;
};

static
char const name[] = "http-client";

static
int header_print(FILE *stream, char const *fmt, http_header_t const *h)
{
  char s[1024];

  msg_header_field_e(s, sizeof(s), (msg_header_t*)h, 0);
  s[sizeof(s) - 1] = '\0';

  if (fmt && strcmp(fmt, "%s"))
    return fprintf(stream, fmt, s);
  if (fputs(s, stream) >= 0)
    return strlen(s);
  return -1;
}

static
int payload_print(FILE *stream, msg_payload_t const *pl)
{
  for (; pl; pl = pl->pl_next) {
    fprintf(stream, "%.*s", (int)pl->pl_len, pl->pl_data);
  }
  return 0;
}

static 
char *read_file(FILE *stream)
{
  int n;
  char *buf;
  off_t used, size;

  if (stream == NULL) {
    errno = EINVAL;
    return NULL;
  }

  /* Read block by block */
  used = 0;
  size = 512;
  buf = malloc(size);

  while (buf) {
    n = fread(buf + used, 1, size - used - 1, stream);
    used += n;
    if (n < size - used - 1) {
      if (feof(stream))
	;
      else if (ferror(stream))
	free(buf), buf = NULL;
      break;
    }
    buf = realloc(buf, 2 * size);
  }

  if (buf)
    if (used < size)
      buf[used] = '\0';

  return buf;
}

char const _usage[] = 
"usage: %s [OPTIONS] url\n"
"       where OPTIONS are as follows\n"
"       --method=name\n"
"       --proxy=url\n"
"       --user=user:password\n"
"       --ua=value\n"
"       --mf=n\n"
"       --pipe\n"
"       --extra\n";

static
void usage(int rc)
{
  fprintf(stderr, _usage, name);
  exit(rc);
}

static int response(context_t *context,
			       nth_client_t *oreq,
			       http_t const *http);

int main(int argc, char *argv[])
{
  su_home_t *home;
  context_t context[1] = {{{SU_HOME_INIT(context)}}};
  http_method_t method;
  char 
    *o_proxy = NULL, 
    *o_user = NULL,
    *o_max_forwards = NULL,
    *o_method_name = "GET",
    *o_user_agent = "http-client/1.0 " "nth/" NTH_VERSION;
  int 
    o_pipe = 0, o_extra = 0;
   
  char *extra = NULL;
  char *v;

#define MATCH(s, o) \
      ((strcmp(s, o) == 0))
#define MATCH1(s, o) \
      ((strncmp(s, o, strlen(o)) == 0) && \
       (v = (s[strlen(o)] ? s + strlen(o) : argv++[1])))
#define MATCH2(s, o) \
      ((strncmp(s, o, strlen(o)) == 0) && \
       (s[strlen(o)] == '=' || s[strlen(o)] == '\0') && \
       (v = s[strlen(o)] ? s + strlen(o) + 1 : argv++[1]))

  while ((v = argv++[1])) {
    if (v[0] != '-')                 { argv--; break; }
    else if (MATCH(v, "-"))          { break; }
    else if (MATCH2(v, "--method"))  { o_method_name = v;  continue; }
    else if (MATCH2(v, "--mf"))      { o_max_forwards = v; continue; }
    else if (MATCH2(v, "--proxy"))   { o_proxy = v;        continue; }
    else if (MATCH2(v, "--user"))    { o_user = v;         continue; }
    else if (MATCH2(v, "--ua"))      { o_user_agent = v;   continue; }
    else if (MATCH(v, "--pipe"))     { o_pipe = 1;         continue; }
    else if (MATCH(v, "--extra"))    { o_extra = 1;        continue; }
    else if (MATCH(v, "--help"))     { usage(0);           continue; }
    else 
      usage(1);
  }

  if (!argv[1])
    usage(1);

  method = http_method_code(o_method_name);

  if (o_user) {
    char *pass = strchr(o_user, ':');
    if (pass) *pass++ = '\0';
    context->c_user = o_user, context->c_pass = pass;
  }

  su_init();

  su_home_init(home = context->c_home);

  if (o_extra) {
    if (isatty(0)) 
      fprintf(stderr, 
	      "Type extra HTTP headers, empty line then HTTP message body "
	      "(^D when complete):\n");
    fflush(stderr);

    extra = read_file(stdin);
  }

  context->c_root = su_root_create(context);

  if (context->c_root) {
    context->c_engine = 
      nth_engine_create(context->c_root, 
			NTHTAG_ERROR_MSG(0),
			TAG_END());

    if (context->c_engine) {
      while ((v = argv++[1])) {
	nth_client_t *clnt;
	clnt = nth_client_tcreate(context->c_engine,
				  response, context, 
				  method, o_method_name,
				  URL_STRING_MAKE(v),
				  NTHTAG_PROXY(o_proxy),
				  HTTPTAG_USER_AGENT_STR(o_user_agent),
				  HTTPTAG_MAX_FORWARDS_STR(o_max_forwards),
				  TPTAG_REUSE(o_pipe),
				  HTTPTAG_HEADER_STR(extra),
				  TAG_END());
	if (clnt)
	  context->c_pending++;
      }

      if (context->c_pending)
	su_root_run(context->c_root); 

      nth_engine_destroy(context->c_engine), context->c_engine = NULL;
    }
    su_root_destroy(context->c_root);
  }

  su_deinit();

  return 0;
}

/** Handle responses to request */
static
int response(context_t *c,
	     nth_client_t *clnt,
	     http_t const *http)
{
  nth_client_t *newclnt = NULL;
  int status;

  if (http) {
    status = http->http_status->st_status;
  } else {
    status = nth_client_status(clnt);
    fprintf(stderr, "HTTP/1.1 %u Error\n", status);
  } 

  if (http && (c->c_pre || status >= 200)) {
    http_header_t *h = (http_header_t *)http->http_status;
    char hname[64];

    for (; h; h = (http_header_t *)h->sh_succ) {
      if (h == (http_header_t *)http->http_payload)
	continue;
      else if (h == (http_header_t *)http->http_separator)
	continue;
      else if (!h->sh_class->hc_name)
	header_print(stdout, NULL, h);
      else if (h->sh_class->hc_name[0]) {
	snprintf(hname, sizeof hname, "%s: %%s\n", h->sh_class->hc_name);
	header_print(stdout, hname, h); 
      } else {
	header_print(stdout, "%s\n", h); 
      }
    }

    printf("\n");
    if (http->http_payload)
      payload_print(stdout, http->http_payload);

    fflush(stdout);
  }

  if (status < 200)
    return 0;

  if (status == 401 && http->http_www_authenticate) {
    char const *user = c->c_user;
    char const *pass = c->c_pass;

    if (!user || !pass) {
      url_t const *url = nth_client_url(clnt);
      if (url) {
	user = url->url_user, pass = url->url_password;
       }
    }

    //if (user && pass &&
    if (
	auc_challenge(&c->c_auth, c->c_home, 
		      http->http_www_authenticate,
		      http_authorization_class) > 0) {
      char const *scheme = NULL;
      char const *realm = NULL;

      scheme = http->http_www_authenticate->au_scheme;
      realm = msg_params_find(http->http_www_authenticate->au_params,
				"realm=");
      if (auc_all_credentials(&c->c_auth, scheme, realm, user, pass)
	  >= 0)
	newclnt = nth_client_tcreate(c->c_engine, 
				     NULL, NULL, HTTP_NO_METHOD, NULL,
				     NTHTAG_AUTHENTICATION(&c->c_auth),
				     NTHTAG_TEMPLATE(clnt),
				     TAG_END());
    }
  }

  if (status == 302 && http->http_location) {
    url_t loc[1];
    
    *loc = *http->http_location->loc_url;

    newclnt = nth_client_tcreate(c->c_engine, NULL, NULL,
				 HTTP_NO_METHOD, 
				 (url_string_t *)loc,
				 NTHTAG_TEMPLATE(clnt),
				 TAG_END());
  }


  if (newclnt)
    c->c_pending++;
  
  nth_client_destroy(clnt);
  if (c->c_pending-- == 1)
    su_root_break(c->c_root);

  return 0;
}