OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [rtems_webserver/] [url.c] - Blame information for rev 543

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * url.c -- Parse URLs
3
 *
4
 * Copyright (c) Go Ahead Software Inc., 1995-1999. All Rights Reserved.
5
 *
6
 * See the file "license.txt" for usage and redistribution license requirements
7
 */
8
 
9
/******************************** Description *********************************/
10
 
11
/*
12
 *      This module parses URLs into their components.
13
 */
14
 
15
/********************************* Includes ***********************************/
16
 
17
#include        "wsIntrn.h"
18
 
19
/********************************* Statics ************************************/
20
/*
21
 *      htmExt is declared in this way to avoid a Linux and Solaris segmentation
22
 *      fault when a constant string is passed to strlower which could change its
23
 *      argument.
24
 */
25
 
26
char_t  htmExt[] = T(".htm");
27
 
28
 
29
/*********************************** Code *************************************/
30
/*
31
 *      Return the mime type for the given URL given a URL.
32
 *      The caller supplies the buffer to hold the result.
33
 *      charCnt is the number of characters the buffer will hold, ascii or UNICODE.
34
 */
35
 
36
char_t *websUrlType(char_t *url, char_t *buf, int charCnt)
37
{
38
        sym_t   *sp;
39
        char_t  *ext, *parsebuf;
40
 
41
        a_assert(url && *url);
42
        a_assert(buf && charCnt > 0);
43
 
44
        if (url == NULL || *url == '\0') {
45
                gstrcpy(buf, T("text/plain"));
46
                return buf;
47
        }
48
        if (websUrlParse(url, &parsebuf, NULL, NULL, NULL, NULL, NULL,
49
                        NULL, &ext) < 0) {
50
                gstrcpy(buf, T("text/plain"));
51
                return buf;
52
        }
53
        strlower(ext);
54
 
55
/*
56
 *      Lookup the mime type symbol table to find the relevant content type
57
 */
58
        if ((sp = symLookup(websMime, ext)) != NULL) {
59
                gstrncpy(buf, sp->content.value.string, charCnt);
60
        } else {
61
                gstrcpy(buf, T("text/plain"));
62
        }
63
        bfree(B_L, parsebuf);
64
        return buf;
65
}
66
 
67
/******************************************************************************/
68
/*
69
 *      Parse the URL. A buffer is allocated to store the parsed URL in *pbuf.
70
 *      This must be freed by the caller. NOTE: tag is not yet fully supported.
71
 */
72
 
73
int websUrlParse(char_t *url, char_t **pbuf, char_t **phost, char_t **ppath,
74
        char_t **pport, char_t **pquery, char_t **pproto, char_t **ptag,
75
        char_t **pext)
76
{
77
        char_t          *tok, *cp, *host, *path, *port, *proto, *tag, *query, *ext;
78
        char_t          *last_delim, *hostbuf, *portbuf, *buf;
79
        int                     c, len, ulen;
80
 
81
        a_assert(url);
82
        a_assert(pbuf);
83
 
84
        ulen = gstrlen(url);
85
/*
86
 *      We allocate enough to store a separate hostname and port number fields.
87
 *      As there are 3 strings in the one buffer, we need room for 3 null chars.
88
 *      We allocate MAX_PORT_LEN char_t's for the port number.
89
 */
90
        len = ulen * 2 + MAX_PORT_LEN + 3;
91
        if ((buf = balloc(B_L, len * sizeof(char_t))) == NULL) {
92
                return -1;
93
        }
94
        portbuf = &buf[len - MAX_PORT_LEN - 1];
95
        hostbuf = &buf[ulen+1];
96
        gstrcpy(buf, url);
97
        url = buf;
98
 
99
/*
100
 *      Convert the current listen port to a string. We use this if the URL has
101
 *      no explicit port setting
102
 */
103
        stritoa(websGetPort(), portbuf, MAX_PORT_LEN);
104
        port = portbuf;
105
        path = T("/");
106
        proto = T("http");
107
        host = T("localhost");
108
        query = T("");
109
        ext = htmExt;
110
        tag = T("");
111
 
112
        if (gstrncmp(url, T("http://"), 7) == 0) {
113
                tok = &url[7];
114
                tok[-3] = '\0';
115
                proto = url;
116
                host = tok;
117
                for (cp = tok; *cp; cp++) {
118
                        if (*cp == '/') {
119
                                break;
120
                        }
121
                        if (*cp == ':') {
122
                                *cp++ = '\0';
123
                                port = cp;
124
                                tok = cp;
125
                        }
126
                }
127
                if ((cp = gstrchr(tok, '/')) != NULL) {
128
/*
129
 *                      If a full URL is supplied, we need to copy the host and port
130
 *                      portions into static buffers.
131
 */
132
                        c = *cp;
133
                        *cp = '\0';
134
                        gstrncpy(hostbuf, host, ulen);
135
                        gstrncpy(portbuf, port, MAX_PORT_LEN);
136
                        *cp = c;
137
                        host = hostbuf;
138
                        port = portbuf;
139
                        path = cp;
140
                        tok = cp;
141
                }
142
 
143
        } else {
144
                path = url;
145
                tok = url;
146
        }
147
 
148
/*
149
 *      Parse the tag and the query
150
 */
151
        if ((cp = gstrchr(tok, '#')) != NULL) {
152
                *cp++ = '\0';
153
                path = tok;
154
                tok = cp;
155
        }
156
        if ((cp = gstrchr(tok, '?')) != NULL) {
157
                *cp++ = '\0';
158
                query = cp;
159
        }
160
 
161
/*
162
 *      Only do the following if asked for the extension
163
 */
164
        if (pext) {
165
                if ((cp = gstrrchr(path, '.')) != NULL) {
166
                        if ((last_delim = gstrrchr(path, '/')) != NULL) {
167
                                if (last_delim > cp) {
168
                                        ext = htmExt;
169
                                } else {
170
                                        ext = cp;
171
                                }
172
                        } else {
173
                                ext = cp;
174
                        }
175
                } else {
176
                        if (path[gstrlen(path) - 1] == '/') {
177
                                ext = htmExt;
178
                        }
179
                }
180
        }
181
 
182
/*
183
 *      Pass back the fields requested (if not NULL)
184
 */
185
        if (phost)
186
                *phost = host;
187
        if (ppath)
188
                *ppath = path;
189
        if (pport)
190
                *pport = port;
191
        if (pproto)
192
                *pproto = proto;
193
        if (pquery)
194
                *pquery = query;
195
        if (ptag)
196
                *ptag = tag;
197
        if (pext)
198
                *pext = ext;
199
        *pbuf = buf;
200
        return 0;
201
}
202
 
203
/******************************************************************************/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.