View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.net;
29  
30  import java.net.URI;
31  import java.nio.charset.Charset;
32  import java.nio.charset.StandardCharsets;
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.BitSet;
36  import java.util.List;
37  
38  import org.apache.hc.core5.http.NameValuePair;
39  import org.apache.hc.core5.http.message.BasicNameValuePair;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.Tokenizer;
42  
43  /**
44   * A collection of utilities for encoding URLs.
45   *
46   * @since 4.0
47   *
48   * @deprecated Use {@link URIBuilder} to parse and format {@link URI}s and
49   * {@link WWWFormCodec} to parse and format {@code application/x-www-form-urlencoded} forms.
50   */
51  @Deprecated
52  public class URLEncodedUtils {
53  
54      private static final char QP_SEP_A = '&';
55      private static final char QP_SEP_S = ';';
56  
57      /**
58       * Returns a list of {@link NameValuePair}s URI query parameters.
59       * By convention, {@code '&'} and {@code ';'} are accepted as parameter separators.
60       *
61       * @param uri input URI.
62       * @param charset parameter charset.
63       * @return list of query parameters.
64       *
65       * @since 4.5
66       */
67      public static List<NameValuePair> parse(final URI uri, final Charset charset) {
68          Args.notNull(uri, "URI");
69          final String query = uri.getRawQuery();
70          if (query != null && !query.isEmpty()) {
71              return parse(query, charset);
72          }
73          return new ArrayList<>(0);
74      }
75  
76      /**
77       * Returns a list of {@link NameValuePair}s URI query parameters.
78       * By convention, {@code '&'} and {@code ';'} are accepted as parameter separators.
79       *
80       * @param s URI query component.
81       * @param charset charset to use when decoding the parameters.
82       * @return list of query parameters.
83       *
84       * @since 4.2
85       */
86      public static List<NameValuePair> parse(final CharSequence s, final Charset charset) {
87          if (s == null) {
88              return new ArrayList<>(0);
89          }
90          return parse(s, charset, QP_SEP_A, QP_SEP_S);
91      }
92  
93      /**
94       * Returns a list of {@link NameValuePair}s parameters.
95       *
96       * @param s input text.
97       * @param charset parameter charset.
98       * @param separators parameter separators.
99       * @return list of query parameters.
100      *
101      * @since 4.4
102      */
103     public static List<NameValuePair> parse(
104             final CharSequence s, final Charset charset, final char... separators) {
105         Args.notNull(s, "Char sequence");
106         final Tokenizer tokenParser = Tokenizer.INSTANCE;
107         final BitSet delimSet = new BitSet();
108         for (final char separator: separators) {
109             delimSet.set(separator);
110         }
111         final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
112         final List<NameValuePair> list = new ArrayList<>();
113         while (!cursor.atEnd()) {
114             delimSet.set('=');
115             final String name = tokenParser.parseToken(s, cursor, delimSet);
116             String value = null;
117             if (!cursor.atEnd()) {
118                 final int delim = s.charAt(cursor.getPos());
119                 cursor.updatePos(cursor.getPos() + 1);
120                 if (delim == '=') {
121                     delimSet.clear('=');
122                     value = tokenParser.parseToken(s, cursor, delimSet);
123                     if (!cursor.atEnd()) {
124                         cursor.updatePos(cursor.getPos() + 1);
125                     }
126                 }
127             }
128             if (!name.isEmpty()) {
129                 list.add(new BasicNameValuePair(
130                         PercentCodec.decode(name, charset, true),
131                         PercentCodec.decode(value, charset, true)));
132             }
133         }
134         return list;
135     }
136 
137     /**
138      * Returns a list of URI path segments.
139      *
140      * @param s URI path component.
141      * @param charset parameter charset.
142      * @return list of segments.
143      *
144      * @since 4.5
145      */
146     public static List<String> parsePathSegments(final CharSequence s, final Charset charset) {
147         return URIBuilder.parsePath(s, charset);
148     }
149 
150     /**
151      * Returns a list of URI path segments.
152      *
153      * @param s URI path component.
154      * @return list of segments.
155      *
156      * @since 4.5
157      */
158     public static List<String> parsePathSegments(final CharSequence s) {
159         return parsePathSegments(s, StandardCharsets.UTF_8);
160     }
161 
162     /**
163      * Returns a string consisting of joint encoded path segments.
164      *
165      * @param segments the segments.
166      * @param charset parameter charset.
167      * @return URI path component
168      *
169      * @since 4.5
170      */
171     public static String formatSegments(final Iterable<String> segments, final Charset charset) {
172         Args.notNull(segments, "Segments");
173         final StringBuilder buf = new StringBuilder();
174         URIBuilder.formatPath(buf, segments, false, charset);
175         return buf.toString();
176     }
177 
178     /**
179      * Returns a string consisting of joint encoded path segments.
180      *
181      * @param segments the segments.
182      * @return URI path component
183      *
184      * @since 4.5
185      */
186     public static String formatSegments(final String... segments) {
187         return formatSegments(Arrays.asList(segments), StandardCharsets.UTF_8);
188     }
189 
190     /**
191      * Returns a String that is suitable for use as an {@code application/x-www-form-urlencoded}
192      * list of parameters in an HTTP PUT or HTTP POST.
193      *
194      * @param parameters  The parameters to include.
195      * @param parameterSeparator The parameter separator, by convention, {@code '&'} or {@code ';'}.
196      * @param charset The encoding to use.
197      * @return An {@code application/x-www-form-urlencoded} string
198      *
199      * @since 4.3
200      */
201     public static String format(
202             final Iterable<? extends NameValuePair> parameters,
203             final char parameterSeparator,
204             final Charset charset) {
205         Args.notNull(parameters, "Parameters");
206         final StringBuilder buf = new StringBuilder();
207         int i = 0;
208         for (final NameValuePair parameter : parameters) {
209             if (i > 0) {
210                 buf.append(parameterSeparator);
211             }
212             PercentCodec.encode(buf, parameter.getName(), charset, URLENCODER, true);
213             if (parameter.getValue() != null) {
214                 buf.append('=');
215                 PercentCodec.encode(buf, parameter.getValue(), charset, URLENCODER, true);
216             }
217             i++;
218         }
219         return buf.toString();
220     }
221 
222     /**
223      * Returns a String that is suitable for use as an {@code application/x-www-form-urlencoded}
224      * list of parameters in an HTTP PUT or HTTP POST.
225      *
226      * @param parameters  The parameters to include.
227      * @param charset The encoding to use.
228      * @return An {@code application/x-www-form-urlencoded} string
229      *
230      * @since 4.2
231      */
232     public static String format(
233             final Iterable<? extends NameValuePair> parameters,
234             final Charset charset) {
235         return format(parameters, QP_SEP_A, charset);
236     }
237 
238     private static final BitSet URLENCODER   = new BitSet(256);
239 
240     static {
241         // unreserved chars
242         // alpha characters
243         for (int i = 'a'; i <= 'z'; i++) {
244             URLENCODER.set(i);
245         }
246         for (int i = 'A'; i <= 'Z'; i++) {
247             URLENCODER.set(i);
248         }
249         // numeric characters
250         for (int i = '0'; i <= '9'; i++) {
251             URLENCODER.set(i);
252         }
253         URLENCODER.set('_'); // these are the characters of the "mark" list
254         URLENCODER.set('-');
255         URLENCODER.set('.');
256         URLENCODER.set('*');
257     }
258 
259 }