Last active
December 10, 2015 20:38
-
-
Save capotej/4489219 to your computer and use it in GitHub Desktop.
A generic http header parser, lifted from Apache's FileUpload class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| * (the "License"); you may not use this file except in compliance with | |
| * the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| import java.util.HashMap; | |
| public class HeaderParser { | |
| /* | |
| This is a public, static version of Apache FileUpload's awesome header parser, with minor modifications to use | |
| a HashMap instead of an internal FileItemsImpl and made public instead of protected. | |
| */ | |
| public static HashMap<String, String> getParsedHeaders(String headerPart) { | |
| final int len = headerPart.length(); | |
| HashMap<String, String> headers = new HashMap<String, String>(); | |
| int start = 0; | |
| for (;;) { | |
| int end = parseEndOfLine(headerPart, start); | |
| if (start == end) { | |
| break; | |
| } | |
| String header = headerPart.substring(start, end); | |
| start = end + 2; | |
| while (start < len) { | |
| int nonWs = start; | |
| while (nonWs < len) { | |
| char c = headerPart.charAt(nonWs); | |
| if (c != ' ' && c != '\t') { | |
| break; | |
| } | |
| ++nonWs; | |
| } | |
| if (nonWs == start) { | |
| break; | |
| } | |
| // Continuation line found | |
| end = parseEndOfLine(headerPart, nonWs); | |
| header += " " + headerPart.substring(nonWs, end); | |
| start = end + 2; | |
| } | |
| parseHeaderLine(headers, header); | |
| } | |
| return headers; | |
| } | |
| public static int parseEndOfLine(String headerPart, int end) { | |
| int index = end; | |
| for (;;) { | |
| int offset = headerPart.indexOf('\r', index); | |
| if (offset == -1 || offset + 1 >= headerPart.length()) { | |
| throw new IllegalStateException( | |
| "Expected headers to be terminated by an empty line."); | |
| } | |
| if (headerPart.charAt(offset + 1) == '\n') { | |
| return offset; | |
| } | |
| index = offset + 1; | |
| } | |
| } | |
| public static void parseHeaderLine(HashMap<String,String> headers, String header) { | |
| final int colonOffset = header.indexOf(':'); | |
| if (colonOffset == -1) { | |
| // This header line is malformed, skip it. | |
| return; | |
| } | |
| String headerName = header.substring(0, colonOffset).trim(); | |
| String headerValue = | |
| header.substring(header.indexOf(':') + 1).trim(); | |
| headers.put(headerName, headerValue); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment