File: c-app2.htm

package info (click to toggle)
aolserver 3.4.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 22,692 kB
  • ctags: 33,612
  • sloc: ansic: 171,340; tcl: 10,218; sh: 3,821; cpp: 2,779; makefile: 2,041; yacc: 1,648; perl: 456; php: 13
file content (453 lines) | stat: -rw-r--r-- 30,074 bytes parent folder | download | duplicates (2)
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<HTML><HEAD>
<TITLE>C Examples -- Example 2: alias</TITLE>
<LINK rel=Previous href="c-app1.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="c-app3.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
  <TR>
    <TD ALIGN=LEFT>
      <A NAME="topofpage"></A> <IMG  SRC="as-c-sm.gif">
    </TD>
    <TD ALIGN=RIGHT>
      <A href="c-app1.htm"><IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
      <A href=toc.htm> <IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
      <A href=master.htm> <IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
      <A href="c-app3.htm"> <IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
      <A name="7983"> </A>
    </TD>
  </TR>
</TABLE>

<a name="14874">
</a><h3>Example 2: alias</h3>
<p><a name="15138">
</a>The following example is an Url-to-file translation module which converts a virtual URL to a physical pathname. This module is much more flexible than the translation routine built into the AOLserver and allows you to map multiple URL prefixes to different physical directories. This has been an often requested feature and the alias module will be the default translation routine in a future release. </p>
<p><a name="15121">
</a>This example can be found in the <code>examples/c/alias</code> directory.</p>
<pre>    <a name="15133"></a>
    <a name="15134"></a>/*
    <a name="15173"></a> * This example module implements a URL-&gt;file aliasing extension to the
    <a name="15174"></a> * AOLserver using the Ns_SetUrlToFileProc function from the C API. Loading
    <a name="15175"></a> * this module enables you to define mappings such as
    <a name="15176"></a> * /oldplace/foo.html=newplace/foo.html.  This module maintains compatibility
    <a name="15177"></a> * with the existing (default) AOLserver UrlToFile function by supporting
    <a name="15178"></a> * UserMapDir and PageRoot configuration variables, both of which are
    <a name="15179"></a> * subsumed by the expressive capabilities of this module. 
    <a name="15180"></a> * 
    <a name="15181"></a> * You can load and configure this module by editing your nsd.ini file as
    <a name="15182"></a> * follows: 
    <a name="15183"></a> * 1) In your [ns\server\server-name\modules] section, add the
    <a name="15184"></a> *    following: 
    <a name="15185"></a> *    alias=alias.dll     ;or alias.so on Unix platforms 
    <a name="15186"></a> * 2) Add a section for the alias module that contains your aliases. E.g.:
    <a name="15187"></a> *    [ns\server\server-name\module\alias] 
    <a name="15188"></a> *    /oldplace/foo.html=newplace/foo.html
    <a name="15189"></a> *    ...
    <a name="15190"></a> * 
    <a name="15191"></a> * Note that you can accomplish the above with the setup interface by using the
    <a name="15192"></a> * "List AOLserver Configuration Sections"  available from the "Setup Home"
    <a name="15193"></a> * page in expert mode, or you can edit the nsd.ini file with your favorite
    <a name="15194"></a> * text editor.
    <a name="15195"></a> * 
    <a name="15196"></a> * When defining maps with key/value pairs, keep the following in mind: 
    <a name="15197"></a> * 1) The key (`/oldplace/foo.html' above) is in "URI space" and the value 
    <a name="15198"></a> *    (`newplace/foo.html' above) is in "file space". 
    <a name="15199"></a> * 2) By default, the file space value (`newplace/foo.html' above) is assumed 
    <a name="15200"></a> *    to be relative to the page root defined for the "server-name"
    <a name="15201"></a> *  &nbsp; &nbsp; &nbsp;server. In "file space" (right side of =), you can also 
    <a name="15202"></a> *    specify an absolute pathname such as /home/user/html/index.html.
    <a name="15203"></a> * So, be careful not to use a leading `/' 
    <a name="15204"></a> *    on the right side of the `=' unless you mean it. 
    <a name="15205"></a> * 3) In "file space", `~username' expands to the user's default directory, and
    <a name="15206"></a> *    `~' looks for a user name in the second element of the URI, substituting
    <a name="15207"></a> *    the user's home directory appropriately.
    <a name="15208"></a> * 4) In "URI space", a leading `/~' mapped to a leading `~' in file space supports
    <a name="15209"></a> *    the common ~user usage in URI space. 
    <a name="15210"></a> *     
    <a name="15211"></a> * Now, let's clear things up with an example.  Note that the comments following each 
    <a name="15212"></a> * mapping show a sample URI input followed by sample output generated by this module.
    <a name="15213"></a> *    [ns\server\server-name\module\alias]
    <a name="15214"></a> *    /user1=~godzilla  ;/user1 -&gt; /home/godzilla
    <a name="15215"></a> *    /user2=~/pages    ;/user2/bambi -&gt; /home/bambi/pages
    <a name="15216"></a> *                      ;OR /user2/bambi/foo -&gt; /home/bambi/pages/foo
    <a name="15217"></a> *    /user3=~          ;/user3/bambi -&gt; /home/bambi
    <a name="15218"></a> *                      ;OR /user3/bambi/html -&gt; /home/bambi/html
    <a name="15219"></a> *    /~=~/pages        ;/~foot -&gt; /home/foot/pages
    <a name="15220"></a> *                      ;OR /~foot/section -&gt; /home/foot/pages/section 
    <a name="15221"></a> *
    <a name="15222"></a> * Note that the last entry obviates the need for the `UserMapDir' configuration
    <a name="15604"></a> &nbsp;* &nbsp;parameter. To provide compatibility with the default Ns_UrlToFile function, we'll 
 &nbsp;* insert a mapping &nbsp;to reflect the UserMapDir if it is set in the nsd.ini file.
    <a name="15614"></a> &nbsp;* If UserMapDir is defined and there is a mapping of the form: /~=~/otherplace,
    <a name="15618"></a> &nbsp;* the mapping takes precedence.
    <a name="15226"></a> *
    <a name="15227"></a> * Another mapping, more pervasive than the previous examples, is the following:
    <a name="15228"></a> * /=/home/newroot
    <a name="15229"></a> * This overrides the PageRoot parameter defined in the server-specific section
    <a name="15619"></a> &nbsp;* of nsd.ini. As you would expect, it also defines the implicit root for all other 
 &nbsp;* relative &nbsp;"file space" (right side of `=') values that appear in other aliases.
    <a name="15232"></a> *
    <a name="15233"></a> */
    <a name="15234"></a>
    <a name="15235"></a>#include "ns.h"
    <a name="15236"></a>#include &lt;assert.h&gt;
    <a name="15237"></a>#include &lt;string.h&gt;
    <a name="15238"></a>#define MAPMETHOD "GET" /* placeholder in the general-purpose URL-space routines */
    <a name="15239"></a>/* The following string is appended to user directories (for compatibility) */
    <a name="15630"></a>#define CONFIG_USERMAPDIR  "UserMapDir" 
    <a name="15240"></a>#define MAX_USERNAME 16
    <a name="15241"></a>
    <a name="15242"></a>/*
    <a name="15243"></a> * This data will be made available to our custom UrlToFile function
    <a name="15244"></a> * (AliasedUrlToFile)
    <a name="15245"></a> */
    <a name="15246"></a>typedef struct {
    <a name="15247"></a>    char       *fromUri;
    <a name="15248"></a>    char       *toUri;
    <a name="15249"></a>}           UriMap;
    <a name="15250"></a>
    <a name="15251"></a>static Ns_UrlToFileProc AliasedUrlToFile;
    <a name="15252"></a>static void            UriMapFree(UriMap * map);
    <a name="15253"></a>static int      TildeReplace(char *hServer, Ns_DString * dsOut, char *in, char *uri);
    <a name="15254"></a>static UriMap  *NewMap(char *from, char *to);
    <a name="15255"></a>
    <a name="17866"></a>
    <a name="17867"></a>
    <a name="17868"></a>/*
    <a name="15256"></a> * The Ns_ModuleVersion exported integer is used to verify this module
    <a name="15257"></a> * version when loaded.  For AOLserver 2.0, 1 (one) is the only valid value
    <a name="15258"></a> * for this variable.
    <a name="15259"></a> */
    <a name="15260"></a>int   Ns_ModuleVersion = 1;
    <a name="15261"></a>
    <a name="15262"></a>/*
    <a name="15263"></a> * The following are global IDs used for server-specific storage and
    <a name="15264"></a> * retrieval of data
    <a name="15265"></a> */
    <a name="15266"></a>static int      idAliases = -1;
    <a name="15267"></a>
    <a name="15268"></a>/*
    <a name="15269"></a> * The Ns_ModuleInit function is the function the AOLserver will call each
    <a name="15270"></a> * time the module is loaded into a server.  The function is passed
    <a name="15271"></a> * two parameters:
    <a name="15272"></a> * 
    <a name="15273"></a> * hServer:   The server `handle' as a string. This is the short name given to
    <a name="15274"></a> * the virutal server such as `server1'.
    <a name="15275"></a> * 
    <a name="15276"></a> * hModule:   The module `handle' as a string. This is the short name given to
    <a name="15277"></a> * the module such as `alias'
    <a name="15278"></a> * 
    <a name="15279"></a> * For example, if this module is known as `alias' and loaded into the `server1'
    <a name="15280"></a> * server with entries similar to the following in the nsd.ini file:
    <a name="15281"></a> * 
    <a name="15282"></a> * [ns\servers] 
    <a name="15283"></a> * server1=My First Server
    <a name="15284"></a> * 
    <a name="15285"></a> * [ns\server1\modules] 
    <a name="15286"></a> * alias=alias.dll
    <a name="15287"></a> * 
    <a name="15288"></a> * This function would be called with "server1" and "alias" as its arguments.
    <a name="15289"></a> * 
    <a name="15290"></a> */
    <a name="15291"></a>int
    <a name="15292"></a>Ns_ModuleInit(char *hServer, char *hModule)
    <a name="15293"></a>{
    <a name="15294"></a>    char       *moduleConfigPath;
    <a name="15295"></a>    char       *serverConfigPath;
    <a name="15296"></a>    char       *userMapDir;
    <a name="15297"></a>    Ns_Set     *aliases;
    <a name="15298"></a>    int         i;
    <a name="15299"></a>    if (idAliases &lt; 0) {
    <a name="15300"></a>        idAliases = Ns_ServerSpecificAlloc();
    <a name="15301"></a>    }
    <a name="15302"></a>    serverConfigPath = Ns_ConfigGetPath(hServer, NULL, NULL);
    <a name="17869"></a>
    <a name="15303"></a>    /* 
    <a name="15642"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* for compatibility with the default UrlToFile, create entry for UserMapDir
    <a name="15640"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* if it is defined 
    <a name="15641"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/
    <a name="15304"></a>    if ((userMapDir = Ns_ConfigGetValue(serverConfigPath, CONFIG_USERMAPDIR)) != 
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL) {
    <a name="15305"></a>        UriMap *map;
    <a name="15306"></a>        Ns_DString dsFilePattern;
    <a name="15307"></a>        Ns_DStringInit(&amp;dsFilePattern);
    <a name="15308"></a>        Ns_DStringVarAppend(&amp;dsFilePattern, "~/", userMapDir, NULL);
    <a name="15309"></a>        map = NewMap("/~", Ns_DStringExport(&amp;dsFilePattern));
    <a name="15310"></a>        Ns_Log(Notice, "Ns_ModuleInit(%s,%s): Mapping %s to %s", hServer, hModule,
    <a name="15311"></a>                         map-&gt;fromUri, map-&gt;toUri);
    <a name="15312"></a>        Ns_UrlSpecificSet(hServer, MAPMETHOD, map-&gt;fromUri, idAliases,
    <a name="15313"></a>                          map, 0, (void (*) (void *)) UriMapFree);
    <a name="15314"></a>    }
    <a name="15315"></a>    if ((moduleConfigPath = Ns_ConfigGetPath(hServer, hModule, NULL)) == NULL) {
    <a name="15316"></a>        Ns_Log(Warning, 
    <a name="15643"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Ns_ModuleInit(%s,%s): No file aliases section found in config file",
    <a name="15317"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hServer, hModule);
    <a name="15318"></a>    } else {
    <a name="15319"></a>        if ((aliases = Ns_ConfigGetSection(moduleConfigPath)) != NULL) {
    <a name="15320"></a>            if (Ns_SetSize(aliases) &gt; 0) {
    <a name="15321"></a>                Ns_DString      dsNormalizedKey;
    <a name="15322"></a>                Ns_DStringInit(&amp;dsNormalizedKey);
    <a name="15323"></a>                /*
    <a name="15324"></a>                 * The following function causes the
    <a name="15325"></a>                 * AOLserver to call our AliasedUrlToFile
    <a name="15326"></a>                 * function instead of its default when
    <a name="15327"></a>                 * mapping a URI to a filename.
    <a name="15328"></a>                 */
    <a name="15329"></a>                Ns_SetUrlToFileProc(hServer, AliasedUrlToFile);
    <a name="15330"></a>                for (i = 0; i &lt; Ns_SetSize(aliases); ++i) {
    <a name="15331"></a>                    UriMap     *map;
    <a name="15332"></a>                    char *value;
    <a name="15333"></a>                    /*
    <a name="15334"></a>                     * Normalize the path so that we can
    <a name="15335"></a>                     * use it for matching in
    <a name="15336"></a>                     * AliasedUrlToFile
    <a name="15337"></a>                     */
    <a name="15338"></a>                    Ns_NormalizePath(&amp;dsNormalizedKey, Ns_SetKey(aliases, i));
    <a name="15339"></a>                    value = Ns_SetValue(aliases, i);
    <a name="15340"></a>                    if ((strncmp(dsNormalizedKey.string, "/~",2)==0) &amp;&amp; 
    <a name="15660"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(value[0] != `~')) {
    <a name="15341"></a>                         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ns_Log(Warning, 
    <a name="15661"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"AliasedUrlToFile(%s): %s-&gt;%s ignored, %s must start with `~'",
    <a name="15342"></a>                            hServer, dsNormalizedKey.string, value, value);
    <a name="15343"></a>                    } else {
    <a name="15344"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map = NewMap(ns_strdup(dsNormalizedKey.string), 
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ns_strdup(value)); 
    <a name="15345"></a>                        Ns_Log(Notice, "Ns_ModuleInit(%s,%s): Mapping %s to %s", 
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hServer, hModule, &nbsp;map-&gt;fromUri, map-&gt;toUri);
    <a name="15347"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ns_UrlSpecificSet(hServer, MAPMETHOD, map-&gt;fromUri,
    <a name="15741"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idAliases, &nbsp;map, 0, (void (*) (void *)) UriMapFree);
    <a name="15349"></a>                    }
    <a name="15350"></a>                    Ns_DStringTrunc(&amp;dsNormalizedKey, 0);
    <a name="15351"></a>                }
    <a name="15352"></a>                Ns_DStringFree(&amp;dsNormalizedKey);
    <a name="15353"></a>            }
    <a name="15354"></a>        }
    <a name="15355"></a>    }
    <a name="15356"></a>    return NS_OK;
    <a name="15357"></a>}
    <a name="15358"></a>
    <a name="15359"></a>/*
    <a name="15360"></a> * This function, registered above via Ns_SetUrlToFileProc, will be called by
    <a name="15361"></a> * the AOLserver when it maps a URL to a file.
    <a name="15362"></a> */
    <a name="15363"></a>static int
    <a name="15364"></a>AliasedUrlToFile(Ns_DString * dest, char *hServer, char *relpath)
    <a name="15365"></a>{
    <a name="15366"></a>    int         retval = NS_OK;
    <a name="15367"></a>    Ns_DString      dsAliasedPath;
    <a name="15368"></a>    UriMap     *map;
    <a name="15369"></a>
    <a name="15370"></a>    assert(relpath != NULL);
    <a name="15371"></a>    assert(dest != NULL);
    <a name="15372"></a>    Ns_DStringInit(&amp;dsAliasedPath);<br>
 &nbsp; &nbsp; &nbsp; &nbsp;/* special handling of `/~' pattern in URI */
    <a name="15373"></a>    if (relpath[0]=='/' &amp;&amp; relpath[1]=='~') { 
    <a name="15374"></a>        if ((map = Ns_UrlSpecificGet(hServer, MAPMETHOD, "/~", idAliases)) == NULL) {
    <a name="15375"></a>            Ns_Log(Error, "AliasedUrlToFile(%s): URI %s has not been aliased",
    <a name="15376"></a>                        hServer, relpath);
    <a name="15377"></a>            retval = NS_ERROR;
    <a name="15378"></a>        } else if (map-&gt;toUri[0]=='~') {
    <a name="15379"></a>            retval = TildeReplace(hServer, &amp;dsAliasedPath, map-&gt;toUri, relpath);
    <a name="15380"></a>        } else {
    <a name="15381"></a>            Ns_Log(Bug, 
    <a name="15782"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"AliasedUrlToFile(%s): In mapping %s-&gt;%s, %s must contain a `~'",
    <a name="15382"></a>                        hServer, map-&gt;fromUri, map-&gt;toUri, map-&gt;toUri);
    <a name="15383"></a>            retval = NS_ERROR;
    <a name="15384"></a>        }
    <a name="15385"></a>    } else {
    <a name="15386"></a>        /*
    <a name="15387"></a>         * Check whether the URI (relpath) has been registered for
    <a name="15388"></a>         * aliasing...
    <a name="15389"></a>         */
    <a name="15390"></a>        if ((map = Ns_UrlSpecificGet(hServer, MAPMETHOD, relpath, idAliases))
    <a name="15783"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;== NULL) {
    <a name="15391"></a>            /* no match, copy relpath to destination */
    <a name="15392"></a>            Ns_DStringAppend(&amp;dsAliasedPath, relpath);
    <a name="15393"></a>        } else {
    <a name="15394"></a>            /* matched, check for `~', exact match or initial subpath match of file 
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pattern*/
    <a name="15395"></a>            int         relpathLen, fromUriLen;
    <a name="15396"></a>            if (map-&gt;toUri[0] == `~') {
    <a name="15397"></a>                retval = TildeReplace(hServer, &amp;dsAliasedPath, map-&gt;toUri, relpath);
    <a name="15398"></a>            } else {
    <a name="15399"></a>                fromUriLen = strlen(map-&gt;fromUri);
    <a name="15400"></a>                relpathLen = strlen(relpath);
    <a name="15401"></a>                if (fromUriLen == relpathLen) {
    <a name="15402"></a>                    /* exact match, use simple substitution */
    <a name="15403"></a>                    Ns_DStringAppend(&amp;dsAliasedPath, map-&gt;toUri);
    <a name="15404"></a>                } else if (fromUriLen &lt; relpathLen) {
    <a name="15405"></a>                    /*
    <a name="15406"></a>                    * initial subpath match, substitute only for length
    <a name="15407"></a>                    * of map-&gt;fromUri
    <a name="15408"></a>                    */
    <a name="15409"></a>                    Ns_DStringVarAppend(&amp;dsAliasedPath, map-&gt;toUri,
    <a name="15410"></a>                            relpath[fromUriLen]=='/' ? "" : "/",
    <a name="15411"></a>                            &amp;relpath[fromUriLen], NULL);
    <a name="15412"></a>                } else {
    <a name="15413"></a>                    Ns_Log(Error, "AliasedUrlToFile(%s): %s to %s mapping is not valid",
    <a name="15414"></a>                        hServer, map-&gt;fromUri, relpath);
    <a name="15415"></a>                    retval = NS_ERROR;
    <a name="15416"></a>                }
    <a name="15417"></a>            }
    <a name="15418"></a>        }
    <a name="15419"></a>    }
    <a name="15420"></a>    if (retval == NS_OK) {
    <a name="15421"></a>        if (Ns_PathIsAbsolute(dsAliasedPath.string) &amp;&amp; (map != NULL)) {
    <a name="15422"></a>            Ns_MakePath(dest, dsAliasedPath.string, NULL);
    <a name="15423"></a>        } else {  /* relative path, use pageroot or / from URI space */
    <a name="15424"></a>            UriMap *map;
    <a name="15425"></a>            char *root;
    <a name="15426"></a>            /* URI `/' takes precedence over pageroot */
    <a name="15427"></a>            if ((map = Ns_UrlSpecificGet(hServer, MAPMETHOD, "/", idAliases))
    <a name="15784"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;== NULL) {
    <a name="15428"></a>                root = Ns_PageRoot(hServer);
    <a name="15429"></a>            } else {
    <a name="15430"></a>                root = map-&gt;toUri;
    <a name="15431"></a>            }
    <a name="15432"></a>            Ns_MakePath(dest, root, dsAliasedPath.string, NULL);
    <a name="15433"></a>        }
    <a name="15434"></a>    }
    <a name="15435"></a>    Ns_DStringFree(&amp;dsAliasedPath);
    <a name="15436"></a>    return retval;
    <a name="15437"></a>}
    <a name="15438"></a>
    <a name="15439"></a>/* 
    <a name="15440"></a> * This function performs the expansion and substitution related to
    <a name="15441"></a> * the `~' character as it appears in the URI and file patterns 
    <a name="15442"></a> * defined in the nsd.ini file, and in the URI input.  In this function,
    <a name="15443"></a> * remember that `filePattern' is the string that appeared on the right
    <a name="15444"></a> * side of the `=' in the nsd.ini file and `uri' is the URI from our current
    <a name="15445"></a> * request, not to be confused with the URI (left of `=') defined in the 
    <a name="15446"></a> * nsd.ini file which is used for pattern matching.
    <a name="15447"></a> *
    <a name="15448"></a> */
    <a name="15449"></a>
    <a name="15450"></a>static int
    <a name="15451"></a>TildeReplace(char *hServer, Ns_DString * dsOut, char *filePattern, char *uri)
    <a name="15452"></a>{
    <a name="15453"></a>    int         retval = NS_OK;
    <a name="15454"></a>    char       *pathRemainder;
    <a name="15455"></a>    char       *nameEnd;
    <a name="15456"></a>    char       *uriTail = `\0';
    <a name="15457"></a>    int         found;
    <a name="15458"></a>    char        user[MAX_USERNAME+1];
    <a name="15459"></a>    int         len;
    <a name="15460"></a>
    <a name="15461"></a>    assert (filePattern[0] == `~');
    <a name="15462"></a>    if (filePattern[1] == `\0' || filePattern[1] == `/') { /*~ or ~/  in filePattern */
    <a name="15463"></a>        char *nameStart;
    <a name="15464"></a>        /* parse out user name, second element in uri OR string following /~ */
    <a name="15465"></a>        if (strncmp(uri, "/~", 2) == 0) {  /* special case mapping of /~* = ~   */
    <a name="15466"></a>            nameStart = &amp;uri[2];
    <a name="15467"></a>            if (*nameStart == `\0') { 
    <a name="15468"></a>                nameStart = NULL;               
    <a name="15469"></a>            }
    <a name="15470"></a>        } else {
    <a name="15789"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* skip past first element */
    <a name="15471"></a>            if ((nameStart = strchr(&amp;uri[1], `/')) != NULL) { 
    <a name="15472"></a>                ++nameStart;
    <a name="15473"></a>            }          
    <a name="15474"></a>        }
    <a name="15475"></a>        if (nameStart == NULL) {
    <a name="15476"></a>            Ns_Log(Error, 
    <a name="15794"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"AliasedUrlToFile:TildeReplace(%s): user name not found in URI: %s", 
    <a name="15477"></a>                           hServer, uri);
    <a name="15478"></a>            retval = NS_ERROR;
    <a name="15479"></a>        } else {
    <a name="15480"></a>            uriTail = strchr(nameStart, `/');   
    <a name="15481"></a>            len = (uriTail==NULL) ? strlen(nameStart) : uriTail - nameStart;
    <a name="15482"></a>            if (len &gt;= sizeof(user)) {
    <a name="15483"></a>                Ns_Log(Warning, 
    <a name="15795"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"AliasedUrlToFile:TildeReplace(%s): URI User name too long", 
    <a name="15484"></a>                                        hServer);
    <a name="15485"></a>                retval = NS_ERROR;
    <a name="15486"></a>            } else {
    <a name="15487"></a>                strncpy(user, nameStart, len);
    <a name="15488"></a>                user[len] = `\0';
    <a name="15489"></a>                found = Ns_GetUserHome(dsOut, user);
    <a name="15490"></a>                pathRemainder = (filePattern[1]=='\0') ? "" : &amp;filePattern[1];
    <a name="15491"></a>            }
    <a name="15492"></a>        }
    <a name="15493"></a>    } else {
    <a name="15494"></a>        uriTail = strchr(&amp;uri[1], `/');   /* skip past substituted field in URI */
    <a name="15495"></a>        if ((nameEnd = strchr(&amp;filePattern[1], `/')) == NULL) {   /*  ~user */
    <a name="15496"></a>            found = Ns_GetUserHome(dsOut, &amp;filePattern[1]);
    <a name="15497"></a>            pathRemainder = "";
    <a name="15498"></a>        } else {             /* ~user/more/stuff */
    <a name="15499"></a>            len = nameEnd - filePattern - 1;
    <a name="15500"></a>            if (len &gt;= sizeof(user)) {
    <a name="15501"></a>                Ns_Log(Warning, 
    <a name="15796"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"AliasedUrlToFile:TildeReplace(%s): Mapped User name too long",
    <a name="15797"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hServer);
    <a name="15502"></a>                retval = NS_ERROR;
    <a name="15503"></a>            } else {
    <a name="15504"></a>                strncpy(user, &amp;filePattern[1], len);
    <a name="15505"></a>                user[len] = `\0';
    <a name="15506"></a>                found = Ns_GetUserHome(dsOut, user);
    <a name="15507"></a>                pathRemainder = nameEnd;
    <a name="15508"></a>            }
    <a name="15509"></a>        }
    <a name="15510"></a>    }
    <a name="15511"></a>    if (retval == NS_OK) {
    <a name="15512"></a>        if (!found) {
    <a name="15513"></a>            Ns_Log(Warning, "AliasedUrlToFile:TildeReplace(%s): Unknown user", 
    <a name="15798"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hServer);
    <a name="15514"></a>            retval = NS_ERROR;
    <a name="15515"></a>        } else {
    <a name="15516"></a>            Ns_DStringAppend(dsOut, pathRemainder);
    <a name="15517"></a>            if (uriTail != NULL) {
    <a name="15518"></a>                Ns_DStringAppend(dsOut, uriTail);
    <a name="15519"></a>            }
    <a name="15520"></a>        }
    <a name="15521"></a>    }
    <a name="15522"></a>    return retval;
    <a name="15523"></a>}
    <a name="15524"></a>
    <a name="15525"></a>
    <a name="17870"></a>
    <a name="17871"></a>
    <a name="17872"></a>
    <a name="17873"></a>
    <a name="17874"></a>static UriMap *
    <a name="15526"></a>NewMap(char *from, char *to)
    <a name="15527"></a>{ 
    <a name="15528"></a>    UriMap *map;
    <a name="15529"></a>    map = ns_malloc(sizeof(UriMap));
    <a name="15530"></a>    map-&gt;fromUri = from;
    <a name="15531"></a>    map-&gt;toUri = to;
    <a name="15532"></a>    return map;
    <a name="15533"></a>}
    <a name="15534"></a>
    <a name="15535"></a>static void
    <a name="15536"></a>UriMapFree(UriMap * map)
    <a name="15537"></a>{
    <a name="15538"></a>    ns_free(map-&gt;fromUri);
    <a name="15539"></a>    ns_free(map-&gt;toUri);
    <a name="15540"></a>    ns_free(map);
    <a name="15541"></a>}
    <a name="15542"></a>
    <a name="15845"></a>
</pre><p>

<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="c-app1.htm">
<IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-app3.htm">
<IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright &copy; 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>