View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http;
20  
21  import java.io.UnsupportedEncodingException;
22  
23  import org.eclipse.jetty.util.MultiMap;
24  import org.eclipse.jetty.util.StringUtil;
25  import org.eclipse.jetty.util.TypeUtil;
26  import org.eclipse.jetty.util.URIUtil;
27  import org.eclipse.jetty.util.UrlEncoded;
28  import org.eclipse.jetty.util.Utf8StringBuffer;
29  
30  public class EncodedHttpURI extends HttpURI
31  {
32      private final String _encoding;
33      
34      public EncodedHttpURI(String encoding)
35      {
36          super();
37          _encoding = encoding;
38      }
39      
40      
41      @Override
42      public String getScheme()
43      {
44          if (_scheme==_authority)
45              return null;
46          int l=_authority-_scheme;
47          if (l==5 && 
48              _raw[_scheme]=='h' && 
49              _raw[_scheme+1]=='t' && 
50              _raw[_scheme+2]=='t' && 
51              _raw[_scheme+3]=='p' )
52              return HttpSchemes.HTTP;
53          if (l==6 && 
54              _raw[_scheme]=='h' && 
55              _raw[_scheme+1]=='t' && 
56              _raw[_scheme+2]=='t' && 
57              _raw[_scheme+3]=='p' && 
58              _raw[_scheme+4]=='s' )
59              return HttpSchemes.HTTPS;
60          
61          return StringUtil.toString(_raw,_scheme,_authority-_scheme-1,_encoding);
62      }
63      
64      @Override
65      public String getAuthority()
66      {
67          if (_authority==_path)
68              return null;
69          return StringUtil.toString(_raw,_authority,_path-_authority,_encoding);
70      }
71      
72      @Override
73      public String getHost()
74      {
75          if (_host==_port)
76              return null;
77          return StringUtil.toString(_raw,_host,_port-_host,_encoding);
78      }
79      
80      @Override
81      public int getPort()
82      {
83          if (_port==_path)
84              return -1;
85          return TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
86      }
87      
88      @Override
89      public String getPath()
90      {
91          if (_path==_param)
92              return null;
93          return StringUtil.toString(_raw,_path,_param-_path,_encoding);
94      }
95      
96      @Override
97      public String getDecodedPath()
98      {
99          if (_path==_param)
100             return null;
101         return URIUtil.decodePath(_raw,_path,_param-_path);
102     }
103     
104     @Override
105     public String getPathAndParam()
106     {
107         if (_path==_query)
108             return null;
109         return StringUtil.toString(_raw,_path,_query-_path,_encoding);
110     }
111     
112     @Override
113     public String getCompletePath()
114     {
115         if (_path==_end)
116             return null;
117         return StringUtil.toString(_raw,_path,_end-_path,_encoding);
118     }
119     
120     @Override
121     public String getParam()
122     {
123         if (_param==_query)
124             return null;
125         return StringUtil.toString(_raw,_param+1,_query-_param-1,_encoding);
126     }
127     
128     @Override
129     public String getQuery()
130     {
131         if (_query==_fragment)
132             return null;
133         return StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding);
134     }
135     
136     @Override
137     public boolean hasQuery()
138     {
139         return (_fragment>_query);
140     }
141     
142     @Override
143     public String getFragment()
144     {
145         if (_fragment==_end)
146             return null;
147         return StringUtil.toString(_raw,_fragment+1,_end-_fragment-1,_encoding);
148     }
149 
150     @Override
151     public void decodeQueryTo(MultiMap parameters) 
152     {
153         if (_query==_fragment)
154             return;
155         UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding),parameters,_encoding);
156     }
157 
158     @Override
159     public void decodeQueryTo(MultiMap parameters, String encoding) 
160         throws UnsupportedEncodingException
161     {
162         if (_query==_fragment)
163             return;
164        
165         if (encoding==null)
166             encoding=_encoding;
167         UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding);
168     }
169     
170     @Override
171     public String toString()
172     {
173         if (_rawString==null)
174             _rawString= StringUtil.toString(_raw,_scheme,_end-_scheme,_encoding);
175         return _rawString;
176     }
177     
178     public void writeTo(Utf8StringBuffer buf)
179     {
180         buf.getStringBuffer().append(toString());
181     }
182     
183 }