aboutsummaryrefslogtreecommitdiff
path: root/src/httpap/http.c
blob: 09116cec7fc750ca139be8d06aadd63205389af2 (plain)
1
2
3
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
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
100
101
102
103
104
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
/*
 * http.c 
 * HTTP parsers.
 *
 * Matej Pfajfar <mp292@cam.ac.uk>
 */

/*
 * Changes :
 * $Log$
 * Revision 1.1  2002/06/26 22:45:50  arma
 * Initial revision
 *
 * Revision 1.2  2002/04/02 14:27:33  badbytes
 * Final finishes.
 *
 * Revision 1.1  2002/03/12 23:46:14  mp292
 * HTTP-related routines.
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "../common/log.h"
#include "../common/utils.h"

#include "http.h"

int http_get_line(int s, unsigned char **line, size_t *len, struct timeval *conn_tout)
{
  int retval =0; /* function return value */
  unsigned char buf[HTTPAP_MAXLEN]; /* line buffer */
  unsigned int buflen = 0; /* length of the received data */
  char got_cr = 0; /* received a CR character and hence expecting a LF */
  unsigned char c; /* input character */

  if (!line || !len) /* invalid parameters */
    return -1;
  
  while(1)
  {
    retval = read_tout(s, &c, 1, MSG_WAITALL, conn_tout);
    if (retval < 1)
      return -1;
    
    if (buflen >= HTTPAP_MAXLEN)
      return -1;
    
    buf[buflen++] = c;
    
    if (got_cr)
    {
      if (c != HTTPAP_LF)
	return -1;
      else
	break;
    }
    else
    {
      if (c == HTTPAP_CR)
	got_cr = 1;
    }
  }
  
  *len = buflen;
  if (buflen)
  {
    *line = (unsigned char *)malloc(buflen+1);
    if (!*line)
      return -1;
    else
    {
      memcpy((void *)*line,(void *)buf,buflen);
      (*line)[buflen] = 0; /* add the terminating null character */
    }
  }
  else
    *line = NULL;

  return 0;
}

int http_get_version(unsigned char *rl, unsigned char **http_ver)
{
  unsigned char *start;
  unsigned char *end;
  
  if (!rl || !http_ver) /* invalid parameters */
    return -1;
  
  start = strrchr(rl, ' ');
  if (!start)
    return -1;
  
  end = strrchr(rl, HTTPAP_CR);
  if (!end)
    return -1;
  
  start++;
  *http_ver = (unsigned char *)malloc(end-start+1);
  if (!*http_ver)
    return -1;
  
  strncpy(*http_ver, start, end-start);
  (*http_ver)[end-start] = 0; /* terminating NULL character */
  
  return 0;
}

int http_get_dest(unsigned char *rl, unsigned char **addr, unsigned char **port)
{
  unsigned char *start;
  unsigned char *end;
  unsigned char *colon;

  if (!rl || !addr || !port) /* invalid parameters */
    return -1;
  
  start = strchr(rl, ' ');
  if (!start)
    return -1;
  start++;
  /* make sure this is really an http:// address */
  if (strncmp(start,"http://",7))
    return -1;
  
  start += 7;
  
  end = strchr(start,'/');
  if (!end)
    return -1;
  
  /* check for a :port in the address */
  colon = strchr(start,':');
  if (colon)
  {
    colon++;
    *port = (unsigned char *)malloc(end-colon+1);
    if (!*port)
      return -1;
    strncpy(*port,colon, end-colon);
    (*port)[end-colon] = 0; /* terminating NULL character */
    end = colon-1;
  }
  else
    *port = NULL;
  
  /* extract the server address */
  *addr = (unsigned char *)malloc(end-start+1);
  if (!*addr)
  {
    if (*port)
      free((void *)*port);
    return -1;
  }
  strncpy(*addr,start, end-start);
  (*addr)[end-start] = 0; /* terminating NULL character */
  
  return 0;
}

int http_get_header_name(unsigned char *rl, unsigned char **hname)
{
  unsigned char *end;
  
  if (!rl || !hname) /* invalid parameters */
    return -1;
  
  end = strchr(rl, ':');
  if (!end)
    return -1;
  
  *hname = (unsigned char *)malloc(end-rl+1);
  if (!*hname)
    return -1;
  
  strncpy(*hname,rl,end-rl);
  (*hname)[end-rl] = 0;
  
  return 0;
}