Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: afe925e1ca8be0027babfa6bbdba3730ce24c8d3 (plain) (blame)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.http;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;
import org.eclipse.jetty.util.Utf8StringBuilder;


/* ------------------------------------------------------------ */
/** Http URI.
 * Parse a HTTP URI from a string or byte array.  Given a URI
 * <code>http://user@host:port/path/info;param?query#fragment</code>
 * this class will split it into the following undecoded optional elements:<ul>
 * <li>{@link #getScheme()} - http:</li>
 * <li>{@link #getAuthority()} - //name@host:port</li>
 * <li>{@link #getHost()} - host</li>
 * <li>{@link #getPort()} - port</li>
 * <li>{@link #getPath()} - /path/info</li>
 * <li>{@link #getParam()} - param</li>
 * <li>{@link #getQuery()} - query</li>
 * <li>{@link #getFragment()} - fragment</li>
 * </ul>
 *
 */
public class HttpURI
{
    private static final byte[] __empty={};
    private final static int
    START=0,
    AUTH_OR_PATH=1,
    SCHEME_OR_PATH=2,
    AUTH=4,
    IPV6=5,
    PORT=6,
    PATH=7,
    PARAM=8,
    QUERY=9,
    ASTERISK=10;

    final Charset _charset;
    boolean _partial=false;
    byte[] _raw=__empty;
    String _rawString;
    int _scheme;
    int _authority;
    int _host;
    int _port;
    int _portValue;
    int _path;
    int _param;
    int _query;
    int _fragment;
    int _end;
    boolean _encoded=false;

    public HttpURI()
    {
        _charset = URIUtil.__CHARSET;
    }

    public HttpURI(Charset charset)
    {
        _charset = charset;
    }

    /* ------------------------------------------------------------ */
    /**
     * @param parsePartialAuth If True, parse auth without prior scheme, else treat all URIs starting with / as paths
     */
    public HttpURI(boolean parsePartialAuth)
    {
        _partial=parsePartialAuth;
        _charset = URIUtil.__CHARSET;
    }

    public HttpURI(String raw)
    {
        _rawString=raw;
        byte[] b = raw.getBytes(StandardCharsets.UTF_8);
        parse(b,0,b.length);
        _charset = URIUtil.__CHARSET;
    }

    public HttpURI(byte[] raw,int offset, int length)
    {
        parse2(raw,offset,length);
        _charset = URIUtil.__CHARSET;
    }

    public HttpURI(URI uri)
    {
        parse(uri.toASCIIString());
        _charset = URIUtil.__CHARSET;
    }

    public void parse(String raw)
    {
        byte[] b = StringUtil.getUtf8Bytes(raw);
        parse2(b,0,b.length);
        _rawString=raw;
    }

    public void parseConnect(String raw)
    {
        byte[] b = StringUtil.getBytes(raw);
        parseConnect(b,0,b.length);
        _rawString=raw;
    }

    public void parse(byte[] raw,int offset, int length)
    {
        _rawString=null;
        parse2(raw,offset,length);
    }


    public void parseConnect(byte[] raw,int offset, int length)
    {
        _rawString=null;
        _encoded=false;
        _raw=raw;
        int i=offset;
        int e=offset+length;
        int state=AUTH;
        _end=offset+length;
        _scheme=offset;
        _authority=offset;
        _host=offset;
        _port=_end;
        _portValue=-1;
        _path=_end;
        _param=_end;
        _query=_end;
        _fragment=_end;

        loop: while (i<e)
        {
            char c=(char)(0xff&_raw[i]);
            int s=i++;

            switch (state)
            {
                case AUTH:
                {
                    switch (c)
                    {
                        case ':':
                        {
                            _port = s;
                            break loop;
                        }
                        case '[':
                        {
                            state = IPV6;
                            break;
                        }
                    }
                    continue;
                }

                case IPV6:
                {
                    switch (c)
                    {
                        case '/':
                        {
                            throw new IllegalArgumentException("No closing ']' for " + new String(_raw,offset,length,_charset));
                        }
                        case ']':
                        {
                            state = AUTH;
                            break;
                        }
                    }

                    continue;
                }
            }
        }

        if (_port<_path)
            _portValue=TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
        else
            throw new IllegalArgumentException("No port");
        _path=offset;
    }


    private void parse2(byte[] raw,int offset, int length)
    {
        _encoded=false;
        _raw=raw;
        int i=offset;
        int e=offset+length;
        int state=START;
        int m=offset;
        _end=offset+length;
        _scheme=offset;
        _authority=offset;
        _host=offset;
        _port=offset;
        _portValue=-1;
        _path=offset;
        _param=_end;
        _query=_end;
        _fragment=_end;
        while (i<e)
        {
            char c=(char)(0xff&_raw[i]);
            int s=i++;

            state: switch (state)
            {
                case START:
                {
                    m=s;
                    switch(c)
                    {
                        case '/':
                            state=AUTH_OR_PATH;
                            break;
                        case ';':
                            _param=s;
                            state=PARAM;
                            break;
                        case '?':
                            _param=s;
                            _query=s;
                            state=QUERY;
                            break;
                        case '#':
                            _param=s;
                            _query=s;
                            _fragment=s;
                            break;
                        case '*':
                            _path=s;
                            state=ASTERISK;
                            break;

                        default:
                            state=SCHEME_OR_PATH;
                    }

                    continue;
                }

                case AUTH_OR_PATH:
                {
                    if ((_partial||_scheme!=_authority) && c=='/')
                    {
                        _host=i;
                        _port=_end;
                        _path=_end;
                        state=AUTH;
                    }
                    else if (c==';' || c=='?' || c=='#')
                    {
                        i--;
                        state=PATH;
                    }
                    else
                    {
                        _host=m;
                        _port=m;
                        state=PATH;
                    }
                    continue;
                }

                case SCHEME_OR_PATH:
                {
                    // short cut for http and https
                    if (length>6 && c=='t')
                    {
                        if (_raw[offset+3]==':')
                        {
                            s=offset+3;
                            i=offset+4;
                            c=':';
                        }
                        else if (_raw[offset+4]==':')
                        {
                            s=offset+4;
                            i=offset+5;
                            c=':';
                        }
                        else if (_raw[offset+5]==':')
                        {
                            s=offset+5;
                            i=offset+6;
                            c=':';
                        }
                    }

                    switch (c)
                    {
                        case ':':
                        {
                            m = i++;
                            _authority = m;
                            _path = m;
                            c = (char)(0xff & _raw[i]);
                            if (c == '/')
                                state = AUTH_OR_PATH;
                            else
                            {
                                _host = m;
                                _port = m;
                                state = PATH;
                            }
                            break;
                        }

                        case '/':
                        {
                            state = PATH;
                            break;
                        }

                        case ';':
                        {
                            _param = s;
                            state = PARAM;
                            break;
                        }

                        case '?':
                        {
                            _param = s;
                            _query = s;
                            state = QUERY;
                            break;
                        }

                        case '#':
                        {
                            _param = s;
                            _query = s;
                            _fragment = s;
                            break;
                        }
                    }
                    continue;
                }

                case AUTH:
                {
                    switch (c)
                    {

                        case '/':
                        {
                            m = s;
                            _path = m;
                            _port = _path;
                            state = PATH;
                            break;
                        }
                        case '@':
                        {
                            _host = i;
                            break;
                        }
                        case ':':
                        {
                            _port = s;
                            state = PORT;
                            break;
                        }
                        case '[':
                        {
                            state = IPV6;
                            break;
                        }
                    }
                    continue;
                }

                case IPV6:
                {
                    switch (c)
                    {
                        case '/':
                        {
                            throw new IllegalArgumentException("No closing ']' for " + new String(_raw,offset,length,_charset));
                        }
                        case ']':
                        {
                            state = AUTH;
                            break;
                        }
                    }

                    continue;
                }

                case PORT:
                {
                    if (c=='/')
                    {
                        m=s;
                        _path=m;
                        if (_port<=_authority)
                            _port=_path;
                        state=PATH;
                    }
                    continue;
                }

                case PATH:
                {
                    switch (c)
                    {
                        case ';':
                        {
                            _param = s;
                            state = PARAM;
                            break;
                        }
                        case '?':
                        {
                            _param = s;
                            _query = s;
                            state = QUERY;
                            break;
                        }
                        case '#':
                        {
                            _param = s;
                            _query = s;
                            _fragment = s;
                            break state;
                        }
                        case '%':
                        {
                            _encoded=true;
                        }
                    }
                    continue;
                }

                case PARAM:
                {
                    switch (c)
                    {
                        case '?':
                        {
                            _query = s;
                            state = QUERY;
                            break;
                        }
                        case '#':
                        {
                            _query = s;
                            _fragment = s;
                            break state;
                        }
                    }
                    continue;
                }

                case QUERY:
                {
                    if (c=='#')
                    {
                        _fragment=s;
                        break state;
                    }
                    continue;
                }

                case ASTERISK:
                {
                    throw new IllegalArgumentException("only '*'");
                }
            }
        }

        if (_port<_path)
            _portValue=TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
    }

    public String getScheme()
    {
        if (_scheme==_authority)
            return null;
        int l=_authority-_scheme;
        if (l==5 &&
                _raw[_scheme]=='h' &&
                _raw[_scheme+1]=='t' &&
                _raw[_scheme+2]=='t' &&
                _raw[_scheme+3]=='p' )
            return HttpScheme.HTTP.asString();
        if (l==6 &&
                _raw[_scheme]=='h' &&
                _raw[_scheme+1]=='t' &&
                _raw[_scheme+2]=='t' &&
                _raw[_scheme+3]=='p' &&
                _raw[_scheme+4]=='s' )
            return HttpScheme.HTTPS.asString();

        return new String(_raw,_scheme,_authority-_scheme-1,_charset);
    }

    public String getAuthority()
    {
        if (_authority==_path)
            return null;
        return new String(_raw,_authority,_path-_authority,_charset);
    }

    public String getHost()
    {
        if (_host==_port)
            return null;
        if (_raw[_host]=='[')
            return new String(_raw,_host+1,_port-_host-2,_charset);
        return new String(_raw,_host,_port-_host,_charset);
    }

    public int getPort()
    {
        return _portValue;
    }

    public String getPath()
    {
        if (_path==_param)
            return null;
        return new String(_raw,_path,_param-_path,_charset);
    }

    public String getDecodedPath()
    {
        if (_path==_param)
            return null;

        Utf8StringBuilder utf8b=null;

        for (int i=_path;i<_param;i++)
        {
            byte b = _raw[i];

            if (b=='%')
            {
                if (utf8b==null)
                {
                    utf8b=new Utf8StringBuilder();
                    utf8b.append(_raw,_path,i-_path);
                }
                
                if ((i+2)>=_param)
                    throw new IllegalArgumentException("Bad % encoding: "+this);
                if (_raw[i+1]=='u')
                {
                    if ((i+5)>=_param)
                        throw new IllegalArgumentException("Bad %u encoding: "+this);
                    try
                    {
                        String unicode = new String(Character.toChars(TypeUtil.parseInt(_raw,i+2,4,16)));
                        utf8b.getStringBuilder().append(unicode);
                        i+=5;
                    }
                    catch(Exception e)
                    {
                        throw new RuntimeException(e);
                    }
                }
                else
                {
                    b=(byte)(0xff&TypeUtil.parseInt(_raw,i+1,2,16));
                    utf8b.append(b);
                    i+=2;
                }
                continue;
            }
            else if (utf8b!=null)
            {
                utf8b.append(b);
            }
        }

        if (utf8b==null)
            return StringUtil.toUTF8String(_raw, _path, _param-_path);
        return utf8b.toString();
    }

    public String getDecodedPath(String encoding)
    {
        return getDecodedPath(Charset.forName(encoding));
    }

    public String getDecodedPath(Charset encoding)
    {
        if (_path==_param)
            return null;

        int length = _param-_path;
        byte[] bytes=null;
        int n=0;

        for (int i=_path;i<_param;i++)
        {
            byte b = _raw[i];

            if (b=='%')
            {
                if (bytes==null)
                {
                    bytes=new byte[length];
                    System.arraycopy(_raw,_path,bytes,0,n);
                }
                
                if ((i+2)>=_param)
                    throw new IllegalArgumentException("Bad % encoding: "+this);
                if (_raw[i+1]=='u')
                {
                    if ((i+5)>=_param)
                        throw new IllegalArgumentException("Bad %u encoding: "+this);

                    try
                    {
                        String unicode = new String(Character.toChars(TypeUtil.parseInt(_raw,i+2,4,16)));
                        byte[] encoded = unicode.getBytes(encoding);
                        System.arraycopy(encoded,0,bytes,n,encoded.length);
                        n+=encoded.length;
                        i+=5;
                    }
                    catch(Exception e)
                    {
                        throw new RuntimeException(e);
                    }
                }
                else
                {
                    b=(byte)(0xff&TypeUtil.parseInt(_raw,i+1,2,16));
                    bytes[n++]=b;
                    i+=2;
                }
                continue;
            }
            else if (bytes==null)
            {
                n++;
                continue;
            }

            bytes[n++]=b;
        }


        if (bytes==null)
            return new String(_raw,_path,_param-_path,encoding);

        return new String(bytes,0,n,encoding);
    }

    public String getPathAndParam()
    {
        if (_path==_query)
            return null;
        return new String(_raw,_path,_query-_path,_charset);
    }

    public String getCompletePath()
    {
        if (_path==_end)
            return null;
        return new String(_raw,_path,_end-_path,_charset);
    }

    public String getParam()
    {
        if (_param==_query)
            return null;
        return new String(_raw,_param+1,_query-_param-1,_charset);
    }

    public String getQuery()
    {
        if (_query==_fragment)
            return null;
        return new String(_raw,_query+1,_fragment-_query-1,_charset);
    }

    public String getQuery(String encoding)
    {
        if (_query==_fragment)
            return null;
        return StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding);
    }

    public boolean hasQuery()
    {
        return (_fragment>_query);
    }

    public String getFragment()
    {
        if (_fragment==_end)
            return null;
        return new String(_raw,_fragment+1,_end-_fragment-1,_charset);
    }

    public void decodeQueryTo(MultiMap<String> parameters)
    {
        if (_query==_fragment)
            return;
        if (_charset.equals(StandardCharsets.UTF_8))
            UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters);
        else
            UrlEncoded.decodeTo(new String(_raw,_query+1,_fragment-_query-1,_charset),parameters,_charset,-1);
    }

    public void decodeQueryTo(MultiMap<String> parameters, String encoding) throws UnsupportedEncodingException
    {
        if (_query==_fragment)
            return;

        if (encoding==null || StringUtil.isUTF8(encoding))
            UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters);
        else
            UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding,-1);
    }

    public void decodeQueryTo(MultiMap<String> parameters, Charset encoding) throws UnsupportedEncodingException
    {
        if (_query==_fragment)
            return;

        if (encoding==null || StandardCharsets.UTF_8.equals(encoding))
            UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters);
        else
            UrlEncoded.decodeTo(new String(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding,-1);
    }

    public void clear()
    {
        _scheme=_authority=_host=_port=_path=_param=_query=_fragment=_end=0;
        _raw=__empty;
        _rawString="";
        _encoded=false;
    }

    @Override
    public String toString()
    {
        if (_rawString==null)
            _rawString=new String(_raw,_scheme,_end-_scheme,_charset);
        return _rawString;
    }

    public void writeTo(Utf8StringBuilder buf)
    {
        buf.append(_raw,_scheme,_end-_scheme);
    }

}

Back to the top