http.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2002 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #include <errno.h>
00024 
00025 #ifdef HAVE_SYS_TYPES_H
00026 #include <sys/types.h>
00027 #endif
00028 
00029 #include <sys/socket.h>
00030 
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 
00035 #ifdef HAVE_NETINET_IN_H
00036 #include <netinet/in.h>
00037 #endif
00038 
00039 #ifdef HAVE_ARPA_INET_H
00040 #include <arpa/inet.h>
00041 #endif
00042 
00043 
00044 #include "protocol.h"
00045 
00046 /* Private prototypes */
00047 static char *get_host_header(Port_T, char *);
00048 
00070 int check_http(Port_T p) {
00071 
00072 #define REQ_LENGTH  1024
00073 
00074   int n;
00075   int status;
00076   char buf[STRLEN];
00077   char msg[STRLEN];
00078   char host[STRLEN];
00079   char proto[STRLEN];
00080   char request[REQ_LENGTH];
00081 
00082   ASSERT(p);
00083 
00084   snprintf(request, REQ_LENGTH,
00085            "GET %s HTTP/1.1\r\n"
00086            "Host: %s\r\n"
00087        "Accept: */*\r\n"
00088        "User-Agent: %s/%s\r\n"
00089            "Connection: close\r\n\r\n",
00090            p->request?p->request:"/",
00091            get_host_header(p, host), prog, VERSION);
00092 
00093   if(port_send(p, request, strlen(request), 0) < 0) {
00094     log("HTTP: error sending data -- %s\n", STRERROR);
00095     return FALSE;
00096   }
00097   
00098   if(port_recv(p, buf, sizeof(buf), 0) <= 0) {
00099     log("HTTP: error receiving data -- %s\n", STRERROR);
00100     return FALSE;
00101   }
00102 
00103   chomp(buf);
00104 
00105   n= sscanf(buf, "%s %d %s", proto, &status, msg);
00106   if(n!=3 || (status >= 400)) {
00107     log("HTTP error: %s\n", buf);
00108     return FALSE;
00109   }
00110 
00111   return TRUE;
00112   
00113 }
00114 
00115 
00116 /* ----------------------------------------------------------------- Private */
00117 
00118 
00123 static char *get_host_header(Port_T p, char *hostbuf) {
00124 
00125   if(! strcmp(LOCALHOST, p->hostname) || inet_aton(p->hostname, NULL)) {
00126 
00127     *hostbuf= 0;
00128 
00129   } else {
00130 
00131     snprintf(hostbuf, STRLEN, "%s:%d", p->hostname, p->port);
00132 
00133   }
00134 
00135   return hostbuf;
00136 
00137 }
00138