Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marianogonzalez/1560407 to your computer and use it in GitHub Desktop.
Save marianogonzalez/1560407 to your computer and use it in GitHub Desktop.
MultipartHttpToMapTransformer aux
protected Map<String, String> parseHeaders(String headerPart)
{
Map<String, String> headers = new HashMap<String, String>();
char buffer[] = new char[1024];
boolean done = false;
int j = 0;
int i;
String header, headerName, headerValue;
try
{
while (!done)
{
i = 0;
// Copy a single line of characters into the buffer,
// omitting trailing CRLF.
while (i < 2 || buffer[i - 2] != '\r' || buffer[i - 1] != '\n')
{
buffer[i++] = headerPart.charAt(j++);
}
header = new String(buffer, 0, i - 2);
if (header.equals(""))
{
done = true;
}
else
{
if (header.indexOf(':') == -1)
{
// This header line is malformed, skip it.
continue;
}
headerName = header.substring(0, header.indexOf(':'))
.trim().toLowerCase();
headerValue =
header.substring(header.indexOf(':') + 1).trim();
if ((String)headers.get(headerName.toLowerCase()) != null)
{
// More that one heder of that name exists,
// append to the list.
headers.put(headerName,
headers.get(headerName.toLowerCase()) + ','
+ headerValue);
}
else
{
headers.put(headerName, headerValue);
}
}
}
}
catch (IndexOutOfBoundsException e)
{
if (logger.isDebugEnabled()) {
logger.debug("Headers were malformed. continue with all that was parsed.");
}
}
return headers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment