~ubuntu-branches/ubuntu/quantal/edbrowse/quantal

« back to all changes in this revision

Viewing changes to src/url.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2010-11-29 12:06:07 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20101129120607-murkfny32efpr1nx
Tags: 3.4.5-0ubuntu1
* New upstream release
* Convert to source format 3.0
  - add debian/format/source
* Update for xulrunner-2.0
  - update debian/control
  - add debian/patches/spidermonkey-2.0.patch for the JSNative changes
  - add debian/patches/series
* Don't call the xulrunner binary to find libmozjs
  - update debian/edbrowse.sh
* Pass -lcrypto and -lreadline to the linker
  - update debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
248
248
        }
249
249
        p = q + 1;
250
250
    }
251
 
    /* @ */
 
251
 
252
252
    q = p + strcspn(p, ":?#/\1");
253
253
    if(host)
254
254
        *host = p;
257
257
    if(*q == ':') {             /* port specified */
258
258
        int n;
259
259
        const char *cc, *pp = q + strcspn(q, "/?#\1");
260
 
        n = strtol(q + 1, (char **)&cc, 10);
261
 
        if(cc != pp || !isdigitByte(q[1])) {
262
 
            setError(MSG_BadPort);
263
 
            return -1;
 
260
        if(pp > q + 1) {
 
261
            n = strtol(q + 1, (char **)&cc, 10);
 
262
            if(cc != pp || !isdigitByte(q[1])) {
 
263
                setError(MSG_BadPort);
 
264
                return -1;
 
265
            }
 
266
            if(port)
 
267
                *port = n;
264
268
        }
265
 
        if(port)
266
 
            *port = n;
267
269
        if(portloc)
268
270
            *portloc = q;
269
271
        q = pp;                 /* up to the slash */
502
504
    return ((url[0] | 0x20) == 'p');
503
505
}
504
506
 
 
507
/*
 
508
 * hasPrefix: return true if s has a prefix of p, false otherwise.
 
509
 */
 
510
static bool
 
511
hasPrefix(char *s, char *p)
 
512
{
 
513
    bool ret = false;
 
514
    if(!p[0])
 
515
        ret = true;             /* Empty string is a prefix of all strings. */
 
516
    else {
 
517
        size_t slen = strlen(s);
 
518
        size_t plen = strlen(p);
 
519
        ret = (plen <= slen) && !strncmp(p, s, plen);
 
520
    }
 
521
    return ret;
 
522
}                               /* hasPrefix */
 
523
 
 
524
/*
 
525
 * copyPathSegment: copy everything from *src, starting with the leftmost
 
526
 * character (a slash), and ending with either the next slash (not included)
 
527
 * or the end of the string.
 
528
 * Advance *src to point to the character succeeding the copied text.
 
529
 */
 
530
static void
 
531
copyPathSegment(char **src, char **dest, int *destlen)
 
532
{
 
533
    int spanlen = strcspn(*src + 1, "/") + 1;
 
534
    stringAndBytes(dest, destlen, *src, spanlen);
 
535
    *src = *src + spanlen;
 
536
}                               /* copyPathSegment */
 
537
 
 
538
/*
 
539
 * Remove the rightmost component of a path,
 
540
 * including the preceding slash, if any.
 
541
 */
 
542
static void
 
543
snipLastSegment(char **path, int *pathLen)
 
544
{
 
545
    char *rightmostSlash = strrchr(*path, '/');
 
546
    if(rightmostSlash == NULL)
 
547
        rightmostSlash = *path;
 
548
    *rightmostSlash = '\0';
 
549
    *pathLen = rightmostSlash - *path;
 
550
}                               /* snipLastSegment */
 
551
 
505
552
static void
506
553
squashDirectories(char *url)
507
554
{
508
555
    char *dd = (char *)getDataURL(url);
509
556
    char *s, *t, *end;
 
557
    char *inPath = NULL;
 
558
    char *outPath;
 
559
    size_t outPathLen = 0;
 
560
    char *rest = NULL;
 
561
 
 
562
    outPath = initString(&outPathLen);
510
563
    if(memEqualCI(url, "javascript:", 11))
511
564
        return;
512
565
    if(!dd || dd == url)
519
572
    if(*dd != '/')
520
573
        i_printfExit(MSG_BadSlash, url);
521
574
    end = dd + strcspn(dd, "?#\1");
522
 
    while(true) {
523
 
        s = strstr(dd, "/./");
524
 
        if(s && s < end) {
525
 
            strcpy(s, s + 2);
526
 
            continue;
527
 
        }
528
 
        s = strstr(dd, "/../");
529
 
        if(!s)
530
 
            break;
531
 
        if(s > end)
532
 
            break;
533
 
        if(s == dd) {
534
 
            strcpy(s, s + 3);
535
 
            continue;
536
 
        }
537
 
        for(t = s - 1; *t != '/'; --t) ;
538
 
        s += 3;
539
 
        strcpy(t, s);
 
575
    rest = cloneString(end);
 
576
    inPath = pullString1(dd, end);
 
577
    s = inPath;
 
578
 
 
579
/* The following algorithm is straight out of RFC 3986, section 5.2.4. */
 
580
/* We can ignore several steps because of a loop invariant: */
 
581
/* After the test, *s is always a slash. */
 
582
    while(*s) {
 
583
        if(hasPrefix(s, "/./"))
 
584
            s += 2;             /* Point s at 2nd slash */
 
585
        else if(!strcmp(s, "/.")) {
 
586
            s[1] = '\0';
 
587
            /* We'll copy the segment "/" on the next iteration. */
 
588
            /* And that will be the final iteration of the loop. */
 
589
        } else if(hasPrefix(s, "/../")) {
 
590
            s += 3;             /* Point s at 2nd slash */
 
591
            snipLastSegment(&outPath, &outPathLen);
 
592
        } else if(!strcmp(s, "/..")) {
 
593
            s[1] = '\0';
 
594
            snipLastSegment(&outPath, &outPathLen);
 
595
            /* As above, copy "/" on the next and final iteration. */
 
596
        } else
 
597
            copyPathSegment(&s, &outPath, &outPathLen);
540
598
    }
 
599
    *dd = '\0';
 
600
    strcat(url, outPath);
 
601
    strcat(url, rest);
 
602
    nzFree(inPath);
 
603
    nzFree(outPath);
 
604
    nzFree(rest);
541
605
}                               /* squashDirectories */
542
606
 
543
607
char *