View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http;
8   
9   import org.mule.transport.http.i18n.HttpMessages;
10  
11  import java.io.UnsupportedEncodingException;
12  import java.net.URLEncoder;
13  import java.util.NoSuchElementException;
14  import java.util.StringTokenizer;
15  
16  import org.apache.commons.httpclient.HttpException;
17  import org.apache.commons.httpclient.HttpVersion;
18  import org.apache.commons.httpclient.ProtocolException;
19  
20  /**
21   * Defines a HTTP request-line, consisting of method name, URI and protocol.
22   */
23  public class RequestLine
24  {
25  
26      private HttpVersion httpversion = null;
27      private String method = null;
28      private String uri = null;
29  
30      public static RequestLine parseLine(final String l) throws HttpException
31      {
32          String method;
33          String uri;
34          String protocol;
35          try
36          {
37              if (l==null)
38              {
39                  throw new ProtocolException(HttpMessages.requestLineIsMalformed(l).getMessage());
40              }
41              StringTokenizer st = new StringTokenizer(l, " ");
42              method = st.nextToken();
43              uri = st.nextToken();
44              protocol = st.nextToken();
45          }
46          catch (NoSuchElementException e)
47          {
48              throw new ProtocolException(HttpMessages.requestLineIsMalformed(l).getMessage());
49          }
50          return new RequestLine(method, uri, protocol);
51      }
52  
53      public RequestLine(final String method, final String uri, final HttpVersion httpversion)
54      {
55          super();
56          if (method == null)
57          {
58              throw new IllegalArgumentException("Method may not be null");
59          }
60          if (uri == null)
61          {
62              throw new IllegalArgumentException("URI may not be null");
63          }
64          if (httpversion == null)
65          {
66              throw new IllegalArgumentException("HTTP version may not be null");
67          }
68          this.method = method;
69          this.uri = encodeIfNeeded(uri);
70          this.httpversion = httpversion;
71      }
72  
73      public RequestLine(final String method, final String uri, final String httpversion)
74          throws ProtocolException
75      {
76          this(method, uri, HttpVersion.parse(httpversion));
77      }
78  
79      /*
80       * prevents XSS attacks
81       */
82      private String encodeIfNeeded(String uri)
83      {
84          if (uri.contains("<") || uri.contains(">"))
85          {
86              try
87              {
88                  return URLEncoder.encode(uri, "UTF-8");
89              }
90              catch (UnsupportedEncodingException e)
91              {
92                  // This exception will never occur as long as the JRE supports UTF-8
93                  throw new RuntimeException(e);
94              }
95          }
96          return uri;
97      }
98  
99      public String getMethod()
100     {
101         return this.method;
102     }
103 
104     public HttpVersion getHttpVersion()
105     {
106         return this.httpversion;
107     }
108 
109     public String getUri()
110     {
111         return this.uri;
112     }
113 
114     public String toString()
115     {
116         StringBuffer sb = new StringBuffer(64);
117         sb.append(this.method);
118         sb.append(" ");
119         sb.append(this.uri);
120         sb.append(" ");
121         sb.append(this.httpversion);
122         return sb.toString();
123     }
124 }