File: url.c

package info (click to toggle)
siege 2.66-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,460 kB
  • ctags: 721
  • sloc: sh: 10,489; ansic: 7,710; makefile: 157
file content (413 lines) | stat: -rw-r--r-- 9,525 bytes parent folder | download
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
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
408
409
410
411
412
413
/**
 * URL Processing
 *
 * Copyright (C) 2000-2007 by
 * Jeffrey Fulmer - <jeff@joedog.org>, et al. 
 * This file is distributed as part of Siege 
 *
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <setup.h>
#include <load.h>
#include <joedog/joedog.h>
#include <joedog/boolean.h>

/*HARDCODED ALERT*/
#define PLENGTH 84  /* length of the *prot[] array */

/** 
 * ALERT: using hardcoded array lengths below,
 * if you change this array, then redefine PLENGTH
 * 
 * Currently http(prot[25]) and https(prot[26]) are 
 * the only supported protocols.  But all w3c supported 
 * protocols are listed for URL evaluation.
 */  
static const char *prot[] = {
  "about:",      "addrbook:",  "acap:",      "afp:",
  "afs:",        "callto:",    "chttp:",     "cid:",
  "clsid:",      "data:",      "date:",      "DAV:",
  "dns:",        "eid:",       "fax:",       "file:",
  "finger:",     "freenet:",   "ftp:",       "gopher:",
  "gsm:",        "h323:",      "h324:",      "hdl:",
  "hnews:",      "http:",      "https:",     "iioploc:",
  "ilu:",        "imap:",      "IOR:",       "irc:",
  "isbn:",       "java:",      "JavaRMI:",   "javascript:",
  "jdbc:",       "ldap:",      "lid:",       "lifn:",
  "livescript:", "lrq:",       "mailto:",    "mailserver:",
  "md5:",        "mid:",       "mocha:",     "modem:",
  "news:",       "nfs:",       "nntp:",      "opaquelocktoken:"
  "path:",       "phone:",     "pop:",       "pop3:",
  "printer:",    "prospero:",  "res:",       "rtsp:",
  "rvp:",        "rwhois:",    "rx:",        "sdp:",
  "sip:",        "shttp:",     "snews:",     "STANF:",
  "t120:",       "tel:",       "telephone:", "telnet:",
  "tip:",        "tn3270:",    "tv:",        "uuid:",
  "urn:",        "vemmi:",     "videotex:",  "view:",
  "wais:",       "whois++:",   "whodp:",     "z39.50r:",
  "z39.50s:"
  
};

/**
 * int value of the length of the protocol 
 * string passed to the function.
 */     
int
protocol_length(char *url)
{
  int x;
  /** 
   * hardcoded protocol length!! see explanation above...
   */
  for(x = 0; x < PLENGTH; x ++){ 
    if(strncasecmp( url, prot[x], strlen( prot[x] )) == 0){
      return strlen(prot[x]);
    }
  } 
  return 0;	
}

/**
 * boolean, returns true if the protocol is 
 * supported by siege, false if it is not.
 */ 
BOOLEAN
is_supported(char* url)
{
  if( strncasecmp( url, prot[25], strlen( prot[25] )) == 0 )
    return TRUE;
#ifdef HAVE_SSL
  if( strncasecmp( url, prot[26], strlen( prot[26] )) == 0 )
    return TRUE;
#endif /* HAVE_SSL */
  
  return FALSE;
}

/**
 * get_protocol
 * returns protocol char*
 */
PROTOCOL
get_protocol(const char *url)
{
  if(strncasecmp(url, prot[25], strlen(prot[25])) == 0)
    return HTTP;
  if(strncasecmp(url, prot[26], strlen(prot[26])) == 0)
    #ifdef HAVE_SSL
      return HTTPS;
    #else
      return HTTP;
    #endif /* HAVE_SSL */
  else
    return UNSUPPORTED;
}

/**
 * get_default_port
 */
int
get_default_port(PROTOCOL p)
{
  if(p == HTTP)
    return 80;
  if(p == HTTPS)
    #ifdef HAVE_SSL
      return 443;
    #else
      return 80;
    #endif /* HAVE_SSL */
  else
    return 80; 
}

/**
 * insert_childid 
 * replaces all '+' characters in POST 
 * data with numbers derived from the process id.
 * return void
 */
void
insert_childid(URL *U, int mypid)
{
  int    i,j;
  char   *c, *f, *l;
  char   pidbuf[6];
 
  sprintf( pidbuf, "%5.5d", mypid );
 
  for( i=0; i<5; i++ ){
    if( U->posttemp ){
      f = strchr(U->posttemp, '+');
      l = strrchr(U->posttemp, '+');
      /* Start at last occurrence of '+' and move to first */
      for (j=sizeof(pidbuf)-1, c=l; c >= f; j--, c--){
        if (*c == '+'){
          *c = pidbuf[j-1];
        }
        if (j == 0){
          j=sizeof(pidbuf)-1;   /* Start over */
        }
      }
      /* Now that we're done, copy the new template to data */
      strcpy(U->postdata, U->posttemp);
    }
  }
}


char *
url_encode(char *str)
{
  int size = 0;
  char *ch, *bk;
  char *p, *buf;
  static char unsafe[]     = "<>{}#%|\"\\^~[]`@:\033";
  static char char2hex[16] = "0123456789ABCDEF";

  bk = ch  = str;
  do{
    if( strchr( unsafe, *ch ))
      size += 2;
    ch++; size ++;
  } while( *ch );

  buf = (char*)malloc( size +1 );
  p   = buf;
  ch  = bk;
  do{
    if( strchr( unsafe, *ch )){
      const char c = *ch;
      *p++ = '%';
      *p++ = char2hex[(c >> 4) & 0xf];
      *p++ = char2hex[c & 0xf];
    }
    else{
      *p++ = *ch;
    }
    ch ++;
  } while( *ch );

  *p = '\0';
  return(buf);
}

/**
 * build_from_template 
 * builds POST data replacing all '*' 
 * characters with random numbers
 * returns void
 */
void
build_from_template(URL *U, int rand)
{
  char   *s=U->posttemp;
  char   *t=U->postdata;
  int    i,j,f,l;
  char   buf[9];
 
  sprintf(buf, "%8.8d", rand);
 
  f = strchr(s, '*')  -s;
  l = strrchr(s, '*') -s;
  /* Start at last occurrence of '+' and move to first */
  for (j=sizeof(buf)-1, i=l; i >= f; j--, i--){
    if (s[i] == '*'){
      t[i] = buf[j-1];
    } else {
      t[i] = s[i];
    }
    if (j == 0){
      j=sizeof(buf)-1;   /* Start over */
    }
  } 
}

/**
 * process_post_data
 * populates URL->postdata with POST information
 */
void
process_post_data(URL *U, char *datap)
{
  for( ; isspace((unsigned int)*datap); datap++ ){
    /* Advance past white space */
  }
  if(*datap == '<'){
    datap++;
    load_file(U, datap);
    return;
  } else {
    U->postdata = xstrdup(datap);
    U->postlen  = strlen(U->postdata);
    U->conttype = xstrdup("application/x-www-form-urlencoded");
    return;
  }
  return;
}

URL *
build_url(char *url, int defaultport, int id)
{
  URL *U;                  /* defined in setup.h        */
  int mark[4] = {0,0,0,0}; /* placement counters.       */
  char *post_cmd=NULL;     /* POST directive for server */
  char *tmp;

  U = xcalloc(sizeof(URL), 1);
  U->urlid = id;

  post_cmd = strstr(url, " POST"); 
  if( post_cmd != NULL ){
    /* How do we deal with handling the multi-headed url_t arrays */
    U->calltype = URL_POST;
    *post_cmd = 0;
    post_cmd += 5;
    process_post_data(U, post_cmd);
  } else {
    U->calltype   = URL_GET;
    U->postdata   = NULL;
    U->posttemp   = NULL;
    U->postlen    = 0;
  }

  if((mark[0] = protocol_length(url)) > 0 && is_supported(url) == TRUE){
    mark[0] += 2;
  } else if((mark[0] = protocol_length(url)) > 0 && is_supported(url) == FALSE){
    U->protocol = UNSUPPORTED;
    mark[0]   += 2;
    joe_warning("unsupported protocol");
    return NULL;
  } else {
    /* we are dealing with who knows what */
    tmp = (char*)strstr( url, "://" );
    if(tmp != NULL){
      mark[0] = (strlen(url) - (strlen(tmp) - 3));
    } else {
      mark[0] = 0;  /* no specified protocol, assuming http: */
    }
  }

  mark[1] = mark[0];
  while(url[mark[1]] && url[mark[1]] != ':' && url[mark[1]] != '/') mark[1]++; 

  if(url[mark[1]] == ':'){
    mark[3] = mark[1];
    while(url[mark[1]] && url[mark[1]] != '/'){
      mark[1]++;
    }
  } else { 
    mark[3] = mark[1]; 
  }

  if(url[mark[1]] == '/'){
     mark[2] = mark[1]; 
  } else { 
    mark[2] = strlen(url); 
  } 

  /* here we piece it all together */
  if( mark[0] == 0 ){
    U->protocol = HTTP;
  } else {
    U->protocol = get_protocol(url);
  }
 
  if(mark[0] != mark[3]){
    U->hostname = (char*)(substring(url, mark[0], (mark[3] - mark[0]))); 
  } else {
    joe_warning( "malformed URL: %s", url );
    return NULL;
  }
    
  if(mark[3] == mark[1]){
    if(defaultport < 0)
      U->port = get_default_port(U->protocol);
    else
      U->port = defaultport;
  } else {
    char *portstr = substring(url, mark[3]+1, (mark[2]-(mark[3]+1)));
    if(portstr != NULL){
      U->port = atoi(portstr);
    } else {
      U->port = 80;
    }
    xfree(portstr); 
  }

  tmp = substring(url, mark[2], strlen(url));
  if(tmp == NULL){
    U->pathname = (char *)xstrdup("/"); 
  } else {
    U->pathname = (char *)xstrdup(tmp); 
  }

  U->pathname = (strlen(U->pathname)==0)?strcpy(U->pathname, "/"):U->pathname; 
  trim(U->pathname);

  snprintf(
    U->url, MAX_URL, "%s%s:%d%s", 
    (U->protocol==HTTP)?"http://":"https://", U->hostname, U->port, U->pathname
  );

  xfree(url); 
  xfree(tmp);
  return(U);
}

URL *
add_redirect(char *url, int port, int id)
{
  char *tmp;

  /**
   * freed in build_url()
   */
  tmp = xstrdup(url);
 
  return build_url(tmp, port, id);
}

/**
 * add_url
 * parses char * then populates and 
 * returns a URL with appropriate data.
 */
URL *
add_url(char *url, int id)
{
  char *tmp;

  /**
   * check string integrity so we
   * can inform the user rather then
   * simply abort with an archaic msg.
   */
  if(!url){
    joe_warning("INVALID URL: <%s>", url);
    display_help();
  } 
  /**
   * freed in build_url()
   */
  tmp = (char*)xstrdup(url);
  return build_url(tmp, -1, id);
}