1 | /* |
---|
2 | * url.c -- Parse URLs |
---|
3 | * |
---|
4 | * Copyright (c) GoAhead Software Inc., 1995-2000. 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 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 query string |
---|
150 | */ |
---|
151 | if ((cp = gstrchr(tok, '?')) != NULL) { |
---|
152 | *cp++ = '\0'; |
---|
153 | query = cp; |
---|
154 | path = tok; |
---|
155 | tok = query; |
---|
156 | } |
---|
157 | |
---|
158 | /* |
---|
159 | * Parse the fragment identifier |
---|
160 | */ |
---|
161 | if ((cp = gstrchr(tok, '#')) != NULL) { |
---|
162 | *cp++ = '\0'; |
---|
163 | if (*query == 0) { |
---|
164 | path = tok; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | /* |
---|
169 | * Only do the following if asked for the extension |
---|
170 | */ |
---|
171 | if (pext) { |
---|
172 | if ((cp = gstrrchr(path, '.')) != NULL) { |
---|
173 | if ((last_delim = gstrrchr(path, '/')) != NULL) { |
---|
174 | if (last_delim > cp) { |
---|
175 | ext = htmExt; |
---|
176 | } else { |
---|
177 | ext = cp; |
---|
178 | } |
---|
179 | } else { |
---|
180 | ext = cp; |
---|
181 | } |
---|
182 | } else { |
---|
183 | if (path[gstrlen(path) - 1] == '/') { |
---|
184 | ext = htmExt; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /* |
---|
190 | * Pass back the fields requested (if not NULL) |
---|
191 | */ |
---|
192 | if (phost) |
---|
193 | *phost = host; |
---|
194 | if (ppath) |
---|
195 | *ppath = path; |
---|
196 | if (pport) |
---|
197 | *pport = port; |
---|
198 | if (pproto) |
---|
199 | *pproto = proto; |
---|
200 | if (pquery) |
---|
201 | *pquery = query; |
---|
202 | if (ptag) |
---|
203 | *ptag = tag; |
---|
204 | if (pext) |
---|
205 | *pext = ext; |
---|
206 | *pbuf = buf; |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | /******************************************************************************/ |
---|