1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
27 #include "curl_memory.h"
28 /* The last #include file should be: */
32 * "Remove Dot Segments"
33 * http://tools.ietf.org/html/rfc3986#section-5.2.4
39 * This function gets a zero-terminated path with dot and dotdot sequences
40 * passed in and strips them off according to the rules in RFC 3986 section
43 * The function handles a query part ('?' + stuff) appended but it expects
44 * that fragments ('#' + stuff) have already been cut off.
48 * an allocated dedotdotified output string
50 char *Curl_dedotdotify(const char *input)
52 size_t inlen = strlen(input);
54 size_t clen = inlen; /* the length of the cloned input */
55 char *out = malloc(inlen+1);
60 return NULL; /* out of memory */
62 /* get a cloned copy of the input */
63 clone = strdup(input);
72 * To handle query-parts properly, we must find it and remove it during the
73 * dotdot-operation and then append it again at the end to the output
76 queryp = strchr(clone, '?');
82 /* A. If the input buffer begins with a prefix of "../" or "./", then
83 remove that prefix from the input buffer; otherwise, */
85 if(!strncmp("./", clone, 2)) {
89 else if(!strncmp("../", clone, 3)) {
94 /* B. if the input buffer begins with a prefix of "/./" or "/.", where
95 "." is a complete path segment, then replace that prefix with "/" in
96 the input buffer; otherwise, */
97 else if(!strncmp("/./", clone, 3)) {
101 else if(!strcmp("/.", clone)) {
107 /* C. if the input buffer begins with a prefix of "/../" or "/..", where
108 ".." is a complete path segment, then replace that prefix with "/" in
109 the input buffer and remove the last segment and its preceding "/" (if
110 any) from the output buffer; otherwise, */
112 else if(!strncmp("/../", clone, 4)) {
115 /* remove the last segment from the output buffer */
116 while(outptr > out) {
121 *outptr = 0; /* zero-terminate where it stops */
123 else if(!strcmp("/..", clone)) {
127 /* remove the last segment from the output buffer */
128 while(outptr > out) {
133 *outptr = 0; /* zero-terminate where it stops */
136 /* D. if the input buffer consists only of "." or "..", then remove
137 that from the input buffer; otherwise, */
139 else if(!strcmp(".", clone) || !strcmp("..", clone)) {
144 /* E. move the first path segment in the input buffer to the end of
145 the output buffer, including the initial "/" character (if any) and
146 any subsequent characters up to, but not including, the next "/"
147 character or the end of the input buffer. */
150 *outptr++ = *clone++;
152 } while(*clone && (*clone != '/'));
160 /* There was a query part, append that to the output. The 'clone' string
161 may now have been altered so we copy from the original input string
162 from the correct index. */
163 size_t oindex = queryp - orgclone;
164 qlen = strlen(&input[oindex]);
165 memcpy(outptr, &input[oindex], qlen+1); /* include the ending zero byte */