Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
HttpUtil |
|
| 6.6;6,6 |
1 | /* |
|
2 | * Copyright 1999,2005 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.xmlrpc.util; |
|
17 | ||
18 | import java.io.IOException; |
|
19 | import java.io.InputStream; |
|
20 | import java.io.UnsupportedEncodingException; |
|
21 | import java.util.Enumeration; |
|
22 | import java.util.StringTokenizer; |
|
23 | ||
24 | import org.apache.ws.commons.util.Base64; |
|
25 | import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl; |
|
26 | import org.apache.xmlrpc.common.XmlRpcStreamConfig; |
|
27 | ||
28 | ||
29 | /** Provides utility functions useful in HTTP communications |
|
30 | */ |
|
31 | 0 | public class HttpUtil { |
32 | /** Creates the Base64 encoded credentials for HTTP Basic Authentication. |
|
33 | * @param pUser User name, or null, if no Basic Authentication is being used. |
|
34 | * @param pPassword Users password, or null, if no Basic Authentication is being used. |
|
35 | * @param pEncoding Encoding being used for conversion of the credential string into a byte array. |
|
36 | * @return Base64 encoded credentials, for use in the HTTP header |
|
37 | * @throws UnsupportedEncodingException The encoding <code>pEncoding</code> is invalid. |
|
38 | */ |
|
39 | public static String encodeBasicAuthentication(String pUser, String pPassword, String pEncoding) throws UnsupportedEncodingException { |
|
40 | 288 | if (pUser == null) { |
41 | 288 | return null; |
42 | } |
|
43 | 0 | String s = pUser + ':' + pPassword; |
44 | 0 | if (pEncoding == null) { |
45 | 0 | pEncoding = XmlRpcStreamConfig.UTF8_ENCODING; |
46 | } |
|
47 | 0 | return new String(Base64.encode(s.getBytes(pEncoding))); |
48 | } |
|
49 | ||
50 | /** Returns, whether the HTTP header value <code>pHeaderValue</code> |
|
51 | * indicates, that GZIP encoding is used or may be used. |
|
52 | * @param pHeaderValue The HTTP header value being parsed. This is typically |
|
53 | * the value of "Content-Encoding", or "Accept-Encoding". |
|
54 | * @return True, if the header value suggests that GZIP encoding is or may |
|
55 | * be used. |
|
56 | */ |
|
57 | public static boolean isUsingGzipEncoding(String pHeaderValue) { |
|
58 | 280 | if (pHeaderValue == null) { |
59 | 280 | return false; |
60 | } |
|
61 | 0 | for (StringTokenizer st = new StringTokenizer(pHeaderValue, ","); st.hasMoreTokens(); ) { |
62 | 0 | String encoding = st.nextToken(); |
63 | 0 | int offset = encoding.indexOf(';'); |
64 | 0 | if (offset >= 0) { |
65 | 0 | encoding = encoding.substring(0, offset); |
66 | } |
|
67 | 0 | if ("gzip".equalsIgnoreCase(encoding.trim())) { |
68 | 0 | return true; |
69 | } |
|
70 | } |
|
71 | 0 | return false; |
72 | } |
|
73 | ||
74 | /** Returns, whether the HTTP header values in <code>pValues</code> |
|
75 | * indicate, that GZIP encoding is used or may be used. |
|
76 | * @param pValues The HTTP header values being parsed. These are typically |
|
77 | * the values of "Content-Encoding", or "Accept-Encoding". |
|
78 | * @return True, if the header values suggests that GZIP encoding is or may |
|
79 | * be used. |
|
80 | */ |
|
81 | public static boolean isUsingGzipEncoding(Enumeration pValues) { |
|
82 | 70 | if (pValues != null) { |
83 | 210 | while (pValues.hasMoreElements()) { |
84 | 70 | if (isUsingGzipEncoding((String) pValues.nextElement())) { |
85 | 0 | return true; |
86 | } |
|
87 | } |
|
88 | } |
|
89 | 70 | return false; |
90 | } |
|
91 | ||
92 | /** Reads a header line from the input stream <code>pIn</code> |
|
93 | * and converts it into a string. |
|
94 | * @param pIn The input stream being read. |
|
95 | * @param pBuffer A buffer being used for temporary storage. |
|
96 | * The buffers length is a limit of the header lines length. |
|
97 | * @return Next header line or null, if no more header lines |
|
98 | * are available. |
|
99 | * @throws IOException Reading the header line failed. |
|
100 | */ |
|
101 | public static String readLine(InputStream pIn, byte[] pBuffer) throws IOException { |
|
102 | int next; |
|
103 | 1015 | int count = 0; |
104 | 23662 | while (true) { |
105 | 24677 | next = pIn.read(); |
106 | 24677 | if (next < 0 || next == '\n') { |
107 | 1015 | break; |
108 | } |
|
109 | 23662 | if (next != '\r') { |
110 | 22647 | pBuffer[count++] = (byte) next; |
111 | } |
|
112 | 23662 | if (count >= pBuffer.length) { |
113 | 0 | throw new IOException ("HTTP Header too long"); |
114 | } |
|
115 | } |
|
116 | 1015 | return new String(pBuffer, 0, count); |
117 | } |
|
118 | ||
119 | /** Parses an "Authorization" header and adds the username and password |
|
120 | * to <code>pConfig</code>. |
|
121 | * @param pConfig The request configuration being created. |
|
122 | * @param pLine The header being parsed, including the "basic" part. |
|
123 | */ |
|
124 | public static void parseAuthorization(XmlRpcHttpRequestConfigImpl pConfig, String pLine) { |
|
125 | 70 | if (pLine == null) { |
126 | 70 | return; |
127 | } |
|
128 | 0 | pLine = pLine.trim(); |
129 | 0 | StringTokenizer st = new StringTokenizer(pLine); |
130 | 0 | if (!st.hasMoreTokens()) { |
131 | 0 | return; |
132 | } |
|
133 | 0 | String type = st.nextToken(); |
134 | 0 | if (!"basic".equalsIgnoreCase(type)) { |
135 | 0 | return; |
136 | } |
|
137 | 0 | if (!st.hasMoreTokens()) { |
138 | 0 | return; |
139 | } |
|
140 | 0 | String auth = st.nextToken(); |
141 | try { |
|
142 | 0 | byte[] c = Base64.decode(auth.toCharArray(), 0, auth.length()); |
143 | 0 | String str = new String(c, pConfig.getBasicEncoding()); |
144 | 0 | int col = str.indexOf(':'); |
145 | 0 | if (col >= 0) { |
146 | 0 | pConfig.setBasicUserName(str.substring(0, col)); |
147 | 0 | pConfig.setBasicPassword(str.substring(col+1)); |
148 | } |
|
149 | 0 | } catch (Throwable ignore) { |
150 | } |
|
151 | 0 | } |
152 | } |