~ubuntu-branches/ubuntu/jaunty/monit/jaunty

« back to all changes in this revision

Viewing changes to protocols/http.c

  • Committer: Bazaar Package Importer
  • Author(s): Steinar H. Gunderson
  • Date: 2006-11-13 13:38:47 UTC
  • mfrom: (3.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20061113133847-s552y2xbe1tiiqgy
Tags: 1:4.8.1-2.1
* Non-maintainer upload.
* monit-lfs.patch: Add LFS support; patch adapted from upstream CVS by
  Michael Mende. (Closes: #395164)
* Build-depend on automake and set some magical cdbs option to enable
  re-automaking, as monit-lfs.patch touches configure.ac.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C), 2000-2005 by the monit project group.
 
2
 * Copyright (C), 2000-2006 by the monit project group.
3
3
 * All Rights Reserved.
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
73
73
 *
74
74
 *  @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
75
75
 *  @author Martin Pala, <martinp@tildeslash.com>
76
 
 *  @version \$Id: http.c,v 1.42 2005/03/23 22:57:34 hauk Exp $
 
76
 *  @version \$Id: http.c,v 1.49 2006/04/27 20:16:03 martinp Exp $
77
77
 *  @file
78
78
 */
79
79
 
135
135
                  "%s\r\n",
136
136
                  request, Util_getHTTPHostHeader(s, host, STRLEN), 
137
137
                  prog, VERSION, get_auth_header(P, auth, STRLEN)) < 0) {
138
 
    log("HTTP: error sending data -- %s\n", STRERROR);
 
138
    LogError("HTTP: error sending data -- %s\n", STRERROR);
139
139
    return FALSE;
140
140
  }
141
141
 
179
179
          
180
180
  default:
181
181
    if(H.status>=400) {
182
 
      log("HTTP error: Server returned status %d\n", H.status);
 
182
      LogError("HTTP error: Server returned status %d\n", H.status);
183
183
      return FALSE;
184
184
    }
185
185
    
187
187
  
188
188
  if(P->url_request && P->url_request->regex) {
189
189
    if(! do_regex(&H, P->url_request)) {
190
 
      log("HTTP error: Failed regular expression test on content"
 
190
      LogError("HTTP error: Failed regular expression test on content"
191
191
          " returned from server\n");
192
192
      return FALSE;
193
193
    }
274
274
static int do_regex(Http_T *H, Request_T R) {
275
275
 
276
276
  int n;
 
277
  int size= 0;
277
278
  int rv= TRUE;
278
 
  unsigned char *buf= NULL;
 
279
  int length= 0;
 
280
  char *buf= NULL;
279
281
#ifdef HAVE_REGEX_H
280
282
  int regex_return;
281
283
#else
287
289
  }
288
290
 
289
291
  if(H->content_length == 0) {
290
 
    log("HTTP error: Cannot test regex -- No content returned from server\n");
 
292
    LogError("HTTP error: Cannot test regex -- No content returned "
 
293
      "from server\n");
291
294
    return FALSE;
292
295
  }
293
296
 
295
298
    H->content_length= HTTP_CONTENT_MAX;
296
299
  else if(H->content_length > HTTP_CONTENT_MAX)
297
300
    H->content_length= HTTP_CONTENT_MAX;
298
 
    
299
 
  buf= xmalloc(H->content_length);
300
 
 
301
 
  n= socket_read(H->s, buf, H->content_length);
302
 
  if(n<=0) {
 
301
  
 
302
  n= 0;
 
303
  size= 0;
 
304
  length= H->content_length;
 
305
  buf= xmalloc(H->content_length + 1);
 
306
  
 
307
  do {
 
308
      n= socket_read(H->s, &buf[size], length);
 
309
      if(n<=0)
 
310
        break;
 
311
      size+= n;
 
312
      length-= n;
 
313
  } while(length>0);
 
314
  
 
315
  if(size==0) {
303
316
    rv= FALSE;
304
 
    log("HTTP: error receiving data -- %s\n", STRERROR);
 
317
    LogError("HTTP: error receiving data -- %s\n", STRERROR);
305
318
    goto error;
306
319
  }
307
 
  buf[n]= 0;
 
320
  buf[size]= 0;
308
321
 
309
322
#ifdef HAVE_REGEX_H
310
323
 
334
347
        break;
335
348
 
336
349
      default:
337
 
        log("HTTP error: Invalid content operator\n");
 
350
        LogError("HTTP error: Invalid content operator\n");
338
351
      }
339
352
        
340
353
#else
358
371
        break;
359
372
 
360
373
      default:
361
 
        log("HTTP error: Invalid content operator\n");
 
374
        LogError("HTTP error: Invalid content operator\n");
362
375
      }
363
376
      
364
377
#endif
373
386
/* -------------------------------------------------------- State management */
374
387
 
375
388
 
 
389
/*
 
390
 * Follow redirect responses from the server. 
 
391
 */
376
392
static int do_redirect(Http_T *H) {
377
393
 
378
394
  char host[STRLEN];
379
395
  char auth[STRLEN]= {0};
380
396
  Port_T P= socket_get_Port(H->s);
381
397
 
 
398
  
 
399
  /*
 
400
   * FIXME: there is a 
 
401
   * BIG shortcoming here since the code only follow redirects on the 
 
402
   * same server. What we need to do is to open a new socket against 
 
403
   * the server in the location header. This may provide problems since 
 
404
   * the socket is owned by the caller. Anyway, do investigate this further.
 
405
   */
 
406
  
382
407
  if(!*H->location) {
383
408
    DEBUG("HTTP error: Missing Location header in response\n");
384
409
    return FALSE;
439
464
  H->content_length= -1;
440
465
 
441
466
  if(! socket_readln(H->s, buf, LINE_SIZE)) {
442
 
    log("HTTP: error receiving data -- %s\n", STRERROR);
 
467
    LogError("HTTP: error receiving data -- %s\n", STRERROR);
443
468
    return FALSE;
444
469
  }
445
470
 
446
471
  Util_chomp(buf);
447
472
 
448
473
  if(sscanf(buf, "%*s %d", &H->status) !=1) {
449
 
    log("HTTP error: cannot parse HTTP status in response: %s\n", buf);
 
474
    LogError("HTTP error: cannot parse HTTP status in response: %s\n", buf);
450
475
    return FALSE;
451
476
  }
452
477
 
459
484
   
460
485
    if(Util_startsWith(buf, "Content-Length")) {
461
486
      if(1 != sscanf(buf, "%*s%*[: ]%ld", &H->content_length)) {
462
 
        log("HTTP error: parsing Content-Length response header '%s'\n", buf);
 
487
        LogError("HTTP error: parsing Content-Length response header '%s'\n",
 
488
          buf);
463
489
        return FALSE;
464
490
      }
465
491
      if(H->content_length < 0) {
466
 
        log("HTTP error: Illegal Content-Length response header '%s'\n", buf);
 
492
        LogError("HTTP error: Illegal Content-Length response header '%s'\n",
 
493
          buf);
467
494
        return FALSE;
468
495
      }
469
496
    }
470
497
    
471
498
    if(Util_startsWith(buf, "Location")) {
472
499
      if(1 != sscanf(buf, "%*s%*[: ]%s", H->location)) {
473
 
        log("HTTP error: parsing Location response header '%s'\n", buf);
 
500
        LogError("HTTP error: parsing Location response header '%s'\n",
 
501
          buf);
474
502
        return FALSE;
475
503
      }
476
504
    }
478
506
    if(Util_startsWith(buf, "Connection")) {
479
507
      char *p= strchr(buf, ':');
480
508
      if(!p) {
481
 
        log("HTTP error: parsing Connection response header '%s'\n", buf);
 
509
        LogError("HTTP error: parsing Connection response header '%s'\n",
 
510
          buf);
482
511
        return FALSE;
483
512
      }
484
513
      Util_trim(++p);
489
518
      char *s;
490
519
      char *p= strchr(buf, ':');
491
520
      if(!p) {
492
 
        log("HTTP error: parsing Cookie response header '%s'\n", buf);
 
521
        LogError("HTTP error: parsing Cookie response header '%s'\n", buf);
493
522
        return FALSE;
494
523
      }
495
524
      Util_trim(++p);