summaryrefslogtreecommitdiffstats
path: root/nanohttp/nanohttp-url.c
blob: 194eba9d1ea7851b8ffb78e0816226fe12621fb8 (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
/******************************************************************
*  $Id: nanohttp-url.c,v 1.2 2006/12/10 19:21:07 m0gg 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
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include "nanohttp-logging.h"
#include "nanohttp-error.h"
#include "nanohttp-url.h"
      
#define HTTP_DEFAULT_PORT		80
#define HTTPS_DEFAULT_PORT		443

static void
_hurl_dump(const struct hurl_t *url)
{
  if (!url)
  {
    log_error1("parameter url is NULL");
    return;
  }

  log_verbose2("PROTOCOL: %d", url->protocol);
  log_verbose2("    HOST: \"%s\"", url->host);
  log_verbose2("    PORT: %d", url->port);
  log_verbose2(" CONTEXT: \"%s\"", url->context);

  return;
}

herror_t
hurl_parse(struct hurl_t *url, const char *urlstr)
{
  int iprotocol;
  int ihost;
  int iport;
  int len;
  int size;
  char tmp[8];
  char protocol[1024];
  struct servent *entry;

  iprotocol = 0;
  len = strlen(urlstr);

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

  if (iprotocol == 0)
  {
    log_error1("no protocol");
    return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol");
  }
  if (iprotocol + 3 >= len)
  {
    log_error1("no host");
    return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host");
  }
  if (urlstr[iprotocol] != ':'
      && urlstr[iprotocol + 1] != '/' && urlstr[iprotocol + 2] != '/')
  {
    log_error1("no protocol");
    return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol");
  }

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

  if (ihost == iprotocol + 1)
  {
    log_error1("no host");
    return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host");
  }

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

  /* find protocol */
  strncpy(protocol, urlstr, iprotocol);
  protocol[iprotocol] = '\0';
  if (!strncasecmp(protocol, "http", 5))
    url->protocol = PROTOCOL_HTTP;
  else if (!strncasecmp(protocol, "https", 6))
    url->protocol = PROTOCOL_HTTPS;
  else
    return herror_new("hurl_parse", URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol \"%s\"", protocol);

  /* find right port */
  if (!(entry = getservbyname(protocol, "tcp")))
  {
    log_warn2("getservbyname(\"%s\", \"tcp\") returned NULL, please edit services database", protocol);

    switch (url->protocol)
    {
    case PROTOCOL_HTTP:
      url->port = HTTP_DEFAULT_PORT;
      break;
    case PROTOCOL_HTTPS:
      url->port = HTTPS_DEFAULT_PORT;
      break;
    }
  }
  else
  {
    url->port = ntohs(entry->s_port);
  }

  size = ihost - iprotocol - 3;
  if (!(url->host = (char *)malloc(size + 1)))
  {
    log_error2("malloc failed (%s)", strerror(errno));
    return herror_new("hurl_parse", URL_ERROR, "malloc failed (%s)", strerror(errno));
  }
  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);
  }

  /* find path */
  len = strlen(urlstr);
  if (len > iport)
  {
    size = len - iport;
    if (!(url->context = (char *)malloc(size + 1)))
    {
      log_error2("malloc failed (%s)", strerror(errno));
      return herror_new("hurl_parse", URL_ERROR, "malloc failed (%s)", strerror(errno));
    }
    strncpy(url->context, &urlstr[iport], size);
    url->context[size] = '\0';
  }
  else
  {
    url->context = strdup("");
  }

  _hurl_dump(url);

  return H_OK;
}

void
hurl_free(struct hurl_t *url)
{
  if (url)
  {
    if (url->host)
      free(url->host);

    if (url->context)
      free(url->context);

    free(url);
  }

  return;
}