author | Michael Krelin <hacker@klever.net> | 2009-09-01 19:59:38 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2009-09-01 19:59:38 (UTC) |
commit | da3fe98e5c75940135fcd9bb45b36eed62d8b4dc (patch) (unidiff) | |
tree | eda546a705a667dd1b62f7eee481b3087fbfdf27 /lib | |
parent | 34d57b6b55b51e4b489a4cafce8874dec558b600 (diff) | |
download | libopkele-da3fe98e5c75940135fcd9bb45b36eed62d8b4dc.zip libopkele-da3fe98e5c75940135fcd9bb45b36eed62d8b4dc.tar.gz libopkele-da3fe98e5c75940135fcd9bb45b36eed62d8b4dc.tar.bz2 |
fixed normalization for the single-character trailing component
Thanks Hiroyuki ARAKI for spotting the bug and providing the fix
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/lib/util.cc b/lib/util.cc index 249eeed..4028697 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -327,97 +327,97 @@ namespace opkele { | |||
327 | const char *nptr = uri.data()+interesting+1; | 327 | const char *nptr = uri.data()+interesting+1; |
328 | char *eptr = 0; | 328 | char *eptr = 0; |
329 | long port = strtol(nptr,&eptr,10); | 329 | long port = strtol(nptr,&eptr,10); |
330 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 330 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
331 | char tmp[8]; | 331 | char tmp[8]; |
332 | snprintf(tmp,sizeof(tmp),":%ld",port); | 332 | snprintf(tmp,sizeof(tmp),":%ld",port); |
333 | rv += tmp; | 333 | rv += tmp; |
334 | } | 334 | } |
335 | if(ni==string::npos) { | 335 | if(ni==string::npos) { |
336 | rv += '/'; return rv; | 336 | rv += '/'; return rv; |
337 | } | 337 | } |
338 | interesting = ni; | 338 | interesting = ni; |
339 | }else if(ic!='/') { | 339 | }else if(ic!='/') { |
340 | rv += '/'; rv += ic; | 340 | rv += '/'; rv += ic; |
341 | qf = true; | 341 | qf = true; |
342 | ++interesting; | 342 | ++interesting; |
343 | } | 343 | } |
344 | string::size_type n = interesting; | 344 | string::size_type n = interesting; |
345 | char tmp[3] = { 0,0,0 }; | 345 | char tmp[3] = { 0,0,0 }; |
346 | stack<string::size_type> psegs; psegs.push(rv.length()); | 346 | stack<string::size_type> psegs; psegs.push(rv.length()); |
347 | string pseg; | 347 | string pseg; |
348 | for(;n<ul;) { | 348 | for(;n<ul;) { |
349 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 349 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
350 | if(unsafe==string::npos) { | 350 | if(unsafe==string::npos) { |
351 | pseg.append(uri,n,ul-n-1); n = ul-1; | 351 | pseg.append(uri,n,ul-n-1); n = ul-1; |
352 | }else{ | 352 | }else{ |
353 | pseg.append(uri,n,unsafe-n); | 353 | pseg.append(uri,n,unsafe-n); |
354 | n = unsafe; | 354 | n = unsafe; |
355 | } | 355 | } |
356 | char c = uri[n++]; | 356 | char c = uri[n++]; |
357 | if(c=='%') { | 357 | if(c=='%') { |
358 | if((n+1)>=ul) | 358 | if((n+1)>=ul) |
359 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 359 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
360 | tmp[0] = uri[n++]; | 360 | tmp[0] = uri[n++]; |
361 | tmp[1] = uri[n++]; | 361 | tmp[1] = uri[n++]; |
362 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 362 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
363 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 363 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
364 | int cc = strtol(tmp,0,16); | 364 | int cc = strtol(tmp,0,16); |
365 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 365 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
366 | pseg += (char)cc; | 366 | pseg += (char)cc; |
367 | else{ | 367 | else{ |
368 | pseg += '%'; | 368 | pseg += '%'; |
369 | pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]); | 369 | pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]); |
370 | } | 370 | } |
371 | }else if(qf) { | 371 | }else if(qf) { |
372 | rv += pseg; rv += c; | 372 | rv += pseg; rv += c; |
373 | pseg.clear(); | 373 | pseg.clear(); |
374 | }else if(n>=ul || strchr("?/#",c)) { | 374 | }else if(n>=ul || strchr("?/#",c)) { |
375 | if(pseg.empty() || pseg==".") { | 375 | if( (unsafe!=string::npos && pseg.empty()) || pseg==".") { |
376 | }else if(pseg=="..") { | 376 | }else if(pseg=="..") { |
377 | if(psegs.size()>1) { | 377 | if(psegs.size()>1) { |
378 | rv.resize(psegs.top()); psegs.pop(); | 378 | rv.resize(psegs.top()); psegs.pop(); |
379 | } | 379 | } |
380 | }else{ | 380 | }else{ |
381 | psegs.push(rv.length()); | 381 | psegs.push(rv.length()); |
382 | if(c!='/') { | 382 | if(c!='/') { |
383 | pseg += c; | 383 | pseg += c; |
384 | qf = true; | 384 | qf = true; |
385 | } | 385 | } |
386 | rv += '/'; rv += pseg; | 386 | rv += '/'; rv += pseg; |
387 | } | 387 | } |
388 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 388 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
389 | rv += '/'; | 389 | rv += '/'; |
390 | if(n<ul) | 390 | if(n<ul) |
391 | qf = true; | 391 | qf = true; |
392 | }else if(strchr("?#",c)) { | 392 | }else if(strchr("?#",c)) { |
393 | if(psegs.size()==1 && psegs.top()==rv.length()) | 393 | if(psegs.size()==1 && psegs.top()==rv.length()) |
394 | rv += '/'; | 394 | rv += '/'; |
395 | if(pseg.empty()) | 395 | if(pseg.empty()) |
396 | rv += c; | 396 | rv += c; |
397 | qf = true; | 397 | qf = true; |
398 | } | 398 | } |
399 | pseg.clear(); | 399 | pseg.clear(); |
400 | }else{ | 400 | }else{ |
401 | pseg += c; | 401 | pseg += c; |
402 | } | 402 | } |
403 | } | 403 | } |
404 | if(!pseg.empty()) { | 404 | if(!pseg.empty()) { |
405 | if(!qf) rv += '/'; | 405 | if(!qf) rv += '/'; |
406 | rv += pseg; | 406 | rv += pseg; |
407 | } | 407 | } |
408 | return rv; | 408 | return rv; |
409 | } | 409 | } |
410 | 410 | ||
411 | string& strip_uri_fragment_part(string& u) { | 411 | string& strip_uri_fragment_part(string& u) { |
412 | string::size_type q = u.find('?'), f = u.find('#'); | 412 | string::size_type q = u.find('?'), f = u.find('#'); |
413 | if(q==string::npos) { | 413 | if(q==string::npos) { |
414 | if(f!=string::npos) | 414 | if(f!=string::npos) |
415 | u.erase(f); | 415 | u.erase(f); |
416 | }else{ | 416 | }else{ |
417 | if(f!=string::npos) { | 417 | if(f!=string::npos) { |
418 | if(f<q) | 418 | if(f<q) |
419 | u.erase(f,q-f); | 419 | u.erase(f,q-f); |
420 | else | 420 | else |
421 | u.erase(f); | 421 | u.erase(f); |
422 | } | 422 | } |
423 | } | 423 | } |