summaryrefslogtreecommitdiffstats
path: root/nanohttp/nanohttp-common.c
blob: bb4cde9c57af781ebb1474c01d1d430c16d0fd08 (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
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
/******************************************************************
 *  $Id: nanohttp-common.c,v 1.2 2003/12/16 13:16:13 snowdrop Exp $
 *
 * CSOAP Project:  A http client/server library in C
 * Copyright (C) 2003  Ferhat Ayaz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * 
 * Email: ayaz@jprogrammer.net
 ******************************************************************/

#include <nanohttp/nanohttp-common.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


static log_level_t loglevel = HLOG_DEBUG;

log_level_t log_set_level(log_level_t level)
{
  log_level_t old = loglevel;
  loglevel = level;
  return old;
}


log_level_t log_get_level()
{
  return loglevel;
}


static
void log_write(log_level_t level, const char *prefix,
	       const char* func, const char *format, va_list ap)
{
  char buffer[1054];
  char buffer2[1054];
  
  if (level < loglevel) return;

  sprintf(buffer, "*%s*: [%s] %s\n", prefix,  func, format);
  vsprintf(buffer2, buffer, ap);
  printf(buffer2);
  fflush(stdout); 
}

void log_debug(const char* FUNC, const char *format, ...)
{
  va_list ap;
  
  va_start(ap, format);
  log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap);
  va_end(ap);
}

void log_info(const char* FUNC, const char *format, ...)
{
  va_list ap;
  
  va_start(ap, format);
  log_write(HLOG_INFO, "INFO", FUNC, format, ap);
  va_end(ap);
}

void log_warn(const char* FUNC, const char *format, ...)
{
  va_list ap;
  
  va_start(ap, format);
  log_write(HLOG_WARN, "WARN", FUNC, format, ap);
  va_end(ap);
}

void log_error(const char* FUNC, const char *format, ...)
{
  va_list ap;
  
  va_start(ap, format);
  log_write(HLOG_ERROR, "ERROR", FUNC, format, ap);
  va_end(ap);
}


hpair_t *hpairnode_new(const char* key, const char* value, hpair_t *next)
{
  hpair_t *pair;

  pair = (hpair_t*)malloc(sizeof(hpair_t));
  pair->key = (char*)malloc(strlen(key)+1);
  pair->value = (char*)malloc(strlen(value)+1);
  pair->next = next;

  strcpy(pair->key, key);
  strcpy(pair->value, value);

  return pair;
}

hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t *next)
{
  hpair_t *pair;
  char *key, *value;
  int c;

  pair = (hpair_t*)malloc(sizeof(hpair_t));
  pair->key = "";
  pair->value = "";
  pair->next = next;

  key = strtok_r(str, delim, &value);
  
  if (key != NULL) {   
    pair->key = (char*)malloc(strlen(key)+1);
    strcpy(pair->key, key);
  }

  if (value != NULL) {
    for (c=0;value[c]==' ';c++); /* skip white space */
    pair->value = (char*)malloc(strlen(&value[c])+1);
    strcpy(pair->value, &value[c]);
  }

  return pair;
}

void hpairnode_free(hpair_t *pair)
{
  if (pair == NULL) return;
  
  free(pair->key);
  free(pair->value);
  
  free(pair);
}


char *hpairnode_get(hpair_t *pair, const char* key)
{
  if (key == NULL) {
    log_error1("key is NULL");
    return NULL;
  }

  while (pair != NULL) {
    if (pair->key != NULL) {
      if (!strcmp(pair->key, key)) {
	return pair->value;
      }
    }
    pair = pair->next;
  }

  return NULL;
}

static
void hurl_dump(const hurl_t *url)
{
  const char *FUNC = "hurl_dump";

  if (url == NULL) {
    printf("(null)\n");
    return ;
  }

  log_debug(FUNC, "PROTOCOL : %s", url->protocol?url->protocol:"(null)");
  log_debug(FUNC, "    HOST : %s", url->host?url->host:"(null)");
  log_debug(FUNC, "    PORT : %d", url->port);
  log_debug(FUNC, " CONTEXT : %s", url->context?url->context:"(null)");
}


hurl_t* hurl_new(const char* urlstr)
{
  int iprotocol;
  int ihost;
  int iport;
  int len;
  int size;
  hurl_t *url;
  char tmp[8];
  const char *FUNC = "hurl_create";
  
  iprotocol = 0;
  len = strlen(urlstr);
  
  /* find protocol */
  while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0')
  {
    iprotocol++;
  }

  if (iprotocol == 0) {
    log_error(FUNC, "no protocol");
    return NULL;
  }

  if (iprotocol + 3 >= len) {
    log_error(FUNC, "no host");
    return NULL;
  }

  if ( urlstr[iprotocol] != ':' 
    && urlstr[iprotocol+1] != '/' 
    && urlstr[iprotocol+2] != '/')
  {
    log_error(FUNC, "no protocol");
    return NULL;
  }

  /* find host */
  ihost = iprotocol + 3;
  while (urlstr[ihost] != ':'
    && urlstr[ihost] != '/'
    && urlstr[ihost] != '\0')
  {
    ihost++;
  }

  if (ihost == iprotocol + 1) {
    log_error(FUNC, "no host");
    return NULL;
  }

  /* find port */
  iport = ihost;
  if (ihost + 1 < len) {
    if (urlstr[ihost] == ':') {
      while (urlstr[iport] != '/' && urlstr[iport] != '\0')  {
       iport++;
      }
    }
  }

  url = (hurl_t*)malloc(sizeof(hurl_t));

  url->protocol = (char*)malloc(sizeof(char)*iprotocol+1);
  strncpy(url->protocol, urlstr, iprotocol);
  url->protocol[iprotocol] = '\0';

  size = ihost - iprotocol - 3;
  url->host = (char*)malloc(sizeof(char)*size);
  strncpy(url->host, &urlstr[iprotocol+3], size);
  url->host[size] = '\0';

  if (iport > ihost)
  {
    size = iport - ihost;
    strncpy(tmp, &urlstr[ihost+1], size);
    url->port = atoi(tmp);
  } else {
    url->port = 80;
  }

  len = strlen(urlstr);
  if (len > iport )
  {
    size = len - iport;
    url->context = (char*)malloc(sizeof(char)*size+1);
    strncpy(url->context, &urlstr[iport], size);
    url->context[size]='\0';
  } else {
    url->context = NULL;
  }

  hurl_dump(url);

  return url;
}


void hurl_free(hurl_t *url)
{
  if (url == NULL) return;

  free(url->protocol);
  free(url->host);
  free(url->context);

  free(url);
}


/* response stuff */

/* -------------------------------------------
   FUNCTION: hresponse_new
 ---------------------------------------------*/
hresponse_t *hresponse_new(const char* buffer)
{
  hresponse_t *res;
  char *s1, *s2, *str;
  hpair_t *pair;

  const char *FUNC = "hresponse_new";

  /* create response object */
  res = (hresponse_t*)malloc(sizeof(hresponse_t));
  res->desc = "";
  res->header = NULL;
  res->body = "";

  /* *** parse spec *** */
  /* [HTTP/1.1 | 1.2] [CODE] [DESC] */
  
  /* stage 1: HTTP spec */
  str = (char*)strtok_r(buffer, " ", &s2);
  s1 = s2;
  if (str == NULL) { log_error(FUNC, "Parse error"); return NULL; }
  
  strncpy(res->spec, str, 10);

  /* stage 2: http code */
  str = (char*)strtok_r(s1, " ", &s2);
  s1 = s2;
  if (str == NULL) { log_error(FUNC, "Parse error"); return NULL; }
  
  res->errcode = atoi(str);

  /* stage 3: description text */
  str = (char*)strtok_r(s1, "\r\n", &s2);
  s1 = s2;
  if (str == NULL) { log_error(FUNC, "Parse error"); return NULL; }

  res->desc = (char*)malloc(strlen(str)+1);
  strcpy(res->desc, str);
  res->desc[strlen(str)] = '\0';

  /* *** parse header *** */
  /* [key]: [value] */
  for (;;) {
    str = strtok_r(s1, "\n", &s2);
    s1 = s2;

    /* check if header ends without body */
    if (str == NULL) {
      return res;
    }

    /* check also for end of header */
    if (!strcmp(str, "\r")) {
      break;
    }

    str[strlen(str)-1] = '\0';
    res->header = hpairnode_parse(str, ":", res->header);
  }

  /* *** Save body *** */
  res->body = s1;

  /* return response object */
  return res;
}


void hresponse_free(hresponse_t *res)
{
  /* not implemented yet!*/
}