| 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 |
#include <uriparser/UriDefsConfig.h> |
| 42 |
#if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE)) |
| 43 |
|
| 44 |
# ifdef URI_ENABLE_ANSI |
| 45 |
# define URI_PASS_ANSI 1 |
| 46 |
# include "UriResolve.c" |
| 47 |
# undef URI_PASS_ANSI |
| 48 |
# endif |
| 49 |
# ifdef URI_ENABLE_UNICODE |
| 50 |
# define URI_PASS_UNICODE 1 |
| 51 |
# include "UriResolve.c" |
| 52 |
# undef URI_PASS_UNICODE |
| 53 |
# endif |
| 54 |
#else |
| 55 |
# ifdef URI_PASS_ANSI |
| 56 |
# include <uriparser/UriDefsAnsi.h> |
| 57 |
# else |
| 58 |
# include <uriparser/UriDefsUnicode.h> |
| 59 |
# include <wchar.h> |
| 60 |
# endif |
| 61 |
|
| 62 |
# ifndef URI_DOXYGEN |
| 63 |
# include <uriparser/Uri.h> |
| 64 |
# include "UriCommon.h" |
| 65 |
# include "UriMemory.h" |
| 66 |
# endif |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static URI_INLINE UriBool URI_FUNC(MergePath)(URI_TYPE(Uri) * absWork, |
| 71 |
const URI_TYPE(Uri) * relAppend, |
| 72 |
UriMemoryManager * memory) { |
| 73 |
URI_TYPE(PathSegment) * sourceWalker; |
| 74 |
URI_TYPE(PathSegment) * destPrev; |
| 75 |
if (relAppend->pathHead == NULL) { |
| 76 |
return URI_TRUE; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
if (absWork->pathHead == NULL) { |
| 81 |
URI_TYPE(PathSegment) * const dup = |
| 82 |
memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
| 83 |
if (dup == NULL) { |
| 84 |
return URI_FALSE; |
| 85 |
} |
| 86 |
dup->next = NULL; |
| 87 |
absWork->pathHead = dup; |
| 88 |
absWork->pathTail = dup; |
| 89 |
} |
| 90 |
absWork->pathTail->text.first = relAppend->pathHead->text.first; |
| 91 |
absWork->pathTail->text.afterLast = relAppend->pathHead->text.afterLast; |
| 92 |
|
| 93 |
|
| 94 |
sourceWalker = relAppend->pathHead->next; |
| 95 |
if (sourceWalker == NULL) { |
| 96 |
return URI_TRUE; |
| 97 |
} |
| 98 |
destPrev = absWork->pathTail; |
| 99 |
|
| 100 |
for (;;) { |
| 101 |
URI_TYPE(PathSegment) * const dup = |
| 102 |
memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
| 103 |
if (dup == NULL) { |
| 104 |
destPrev->next = NULL; |
| 105 |
absWork->pathTail = destPrev; |
| 106 |
return URI_FALSE; |
| 107 |
} |
| 108 |
dup->text = sourceWalker->text; |
| 109 |
destPrev->next = dup; |
| 110 |
|
| 111 |
if (sourceWalker->next == NULL) { |
| 112 |
absWork->pathTail = dup; |
| 113 |
absWork->pathTail->next = NULL; |
| 114 |
break; |
| 115 |
} |
| 116 |
destPrev = dup; |
| 117 |
sourceWalker = sourceWalker->next; |
| 118 |
} |
| 119 |
|
| 120 |
return URI_TRUE; |
| 121 |
} |
| 122 |
|
| 123 |
static int URI_FUNC(ResolveAbsolutePathFlag)(URI_TYPE(Uri) * absWork, |
| 124 |
UriMemoryManager * memory) { |
| 125 |
if (absWork == NULL) { |
| 126 |
return URI_ERROR_NULL; |
| 127 |
} |
| 128 |
|
| 129 |
if (URI_FUNC(HasHost)(absWork) && absWork->absolutePath) { |
| 130 |
|
| 131 |
if (absWork->pathHead == NULL) { |
| 132 |
URI_TYPE(PathSegment) * const segment = |
| 133 |
memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
| 134 |
if (segment == NULL) { |
| 135 |
return URI_ERROR_MALLOC; |
| 136 |
} |
| 137 |
segment->text.first = URI_FUNC(SafeToPointTo); |
| 138 |
segment->text.afterLast = URI_FUNC(SafeToPointTo); |
| 139 |
segment->next = NULL; |
| 140 |
|
| 141 |
absWork->pathHead = segment; |
| 142 |
absWork->pathTail = segment; |
| 143 |
} |
| 144 |
|
| 145 |
absWork->absolutePath = URI_FALSE; |
| 146 |
} |
| 147 |
|
| 148 |
return URI_SUCCESS; |
| 149 |
} |
| 150 |
|
| 151 |
static int URI_FUNC(AddBaseUriImpl)(URI_TYPE(Uri) * absDest, |
| 152 |
const URI_TYPE(Uri) * relSource, |
| 153 |
const URI_TYPE(Uri) * absBase, |
| 154 |
UriResolutionOptions options, |
| 155 |
UriMemoryManager * memory) { |
| 156 |
UriBool relSourceHasScheme; |
| 157 |
|
| 158 |
if (absDest == NULL) { |
| 159 |
return URI_ERROR_NULL; |
| 160 |
} |
| 161 |
URI_FUNC(ResetUri)(absDest); |
| 162 |
|
| 163 |
if ((relSource == NULL) || (absBase == NULL)) { |
| 164 |
return URI_ERROR_NULL; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
if (absBase->scheme.first == NULL) { |
| 169 |
return URI_ERROR_ADDBASE_REL_BASE; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
{ |
| 174 |
{ |
| 175 |
{ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
relSourceHasScheme = |
| 182 |
(relSource->scheme.first != NULL) ? URI_TRUE : URI_FALSE; |
| 183 |
if ((options & URI_RESOLVE_IDENTICAL_SCHEME_COMPAT) |
| 184 |
&& (absBase->scheme.first != NULL) |
| 185 |
&& (relSource->scheme.first != NULL) |
| 186 |
&& (0 |
| 187 |
== URI_FUNC(CompareRange)(&(absBase->scheme), |
| 188 |
&(relSource->scheme)))) { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
relSourceHasScheme = URI_FALSE; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if (relSourceHasScheme) { |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
absDest->scheme = relSource->scheme; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) { |
| 210 |
return URI_ERROR_MALLOC; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) { |
| 216 |
return URI_ERROR_MALLOC; |
| 217 |
} |
| 218 |
if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) { |
| 219 |
return URI_ERROR_MALLOC; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
absDest->query = relSource->query; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
} else { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if (URI_FUNC(HasHost)(relSource)) { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) { |
| 237 |
return URI_ERROR_MALLOC; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) { |
| 243 |
return URI_ERROR_MALLOC; |
| 244 |
} |
| 245 |
if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) { |
| 246 |
return URI_ERROR_MALLOC; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
absDest->query = relSource->query; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
} else { |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
if (!URI_FUNC(CopyAuthority)(absDest, absBase, memory)) { |
| 260 |
return URI_ERROR_MALLOC; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if (relSource->pathHead == NULL && !relSource->absolutePath) { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) { |
| 270 |
return URI_ERROR_MALLOC; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
if (relSource->query.first != NULL) { |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
absDest->query = relSource->query; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
} else { |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
absDest->query = absBase->query; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
} else { |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
if (relSource->absolutePath) { |
| 300 |
int res; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) { |
| 305 |
return URI_ERROR_MALLOC; |
| 306 |
} |
| 307 |
res = URI_FUNC(ResolveAbsolutePathFlag)(absDest, memory); |
| 308 |
if (res != URI_SUCCESS) { |
| 309 |
return res; |
| 310 |
} |
| 311 |
if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, |
| 312 |
memory)) { |
| 313 |
return URI_ERROR_MALLOC; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
} else { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) { |
| 323 |
return URI_ERROR_MALLOC; |
| 324 |
} |
| 325 |
if (!URI_FUNC(MergePath)(absDest, relSource, memory)) { |
| 326 |
return URI_ERROR_MALLOC; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, |
| 332 |
memory)) { |
| 333 |
return URI_ERROR_MALLOC; |
| 334 |
} |
| 335 |
|
| 336 |
if (!URI_FUNC(FixAmbiguity)(absDest, memory)) { |
| 337 |
return URI_ERROR_MALLOC; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
absDest->query = relSource->query; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
} |
| 350 |
URI_FUNC(FixEmptyTrailSegment)(absDest, memory); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
absDest->scheme = absBase->scheme; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
absDest->fragment = relSource->fragment; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
return URI_SUCCESS; |
| 371 |
} |
| 372 |
|
| 373 |
int URI_FUNC(AddBaseUri)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource, |
| 374 |
const URI_TYPE(Uri) * absBase) { |
| 375 |
const UriResolutionOptions options = URI_RESOLVE_STRICTLY; |
| 376 |
return URI_FUNC(AddBaseUriEx)(absDest, relSource, absBase, options); |
| 377 |
} |
| 378 |
|
| 379 |
int URI_FUNC(AddBaseUriEx)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource, |
| 380 |
const URI_TYPE(Uri) * absBase, UriResolutionOptions options) { |
| 381 |
return URI_FUNC(AddBaseUriExMm)(absDest, relSource, absBase, options, NULL); |
| 382 |
} |
| 383 |
|
| 384 |
int URI_FUNC(AddBaseUriExMm)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource, |
| 385 |
const URI_TYPE(Uri) * absBase, UriResolutionOptions options, |
| 386 |
UriMemoryManager * memory) { |
| 387 |
int res; |
| 388 |
|
| 389 |
URI_CHECK_MEMORY_MANAGER(memory); |
| 390 |
|
| 391 |
res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, options, memory); |
| 392 |
if ((res != URI_SUCCESS) && (absDest != NULL)) { |
| 393 |
URI_FUNC(FreeUriMembersMm)(absDest, memory); |
| 394 |
} |
| 395 |
return res; |
| 396 |
} |
| 397 |
|
| 398 |
#endif |