com.sun.grizzly.jruby.HttpHeaderParser Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.grizzly.jruby;
import com.sun.grizzly.tcp.http11.Constants;
import com.sun.grizzly.tcp.http11.GrizzlyResponse;
import com.sun.grizzly.util.buf.MessageBytes;
import com.sun.grizzly.util.http.MimeHeaders;
import java.io.IOException;
class HttpHeaderParser {
private final GrizzlyResponse response;
private int pos;
public HttpHeaderParser(GrizzlyResponse response) {
this.response = response;
}
/**
* Parses the HTTP headers from the given buf and add them to {@link com.sun.grizzly.util.http.MimeHeaders}.
* @return current position in the buf, after \r\n
*/
public int parseHeaders(byte[] buf, int offset, int len) throws IOException {
pos = offset;
while (parseHeader(buf, len)) {
}
return pos;
}
private boolean parseHeader(final byte[] buf, final int len) throws IOException {
String fieldName = null;
String fieldValue;
//
// Check for blank line
//
if (pos >= len) return false;
byte chr = 0;
while (pos < len) {
chr = buf[pos];
if ((chr == Constants.CR) || (chr == Constants.LF)) {
chr = buf[pos + 1];
if (chr == Constants.LF) {
pos +=2;
return false;
}
} else {
break;
}
pos++;
}
// Mark the current buffer position
int start = pos;
//
// Reading the header name
// Header name is always US-ASCII
//
boolean colon = false;
while (!colon && pos < len) {
if (buf[pos] == Constants.COLON) {
colon = true;
fieldName = new String(buf, start, pos - start).trim();
// headerValue = headers.addValue(buf, start, pos - start);
}
chr = buf[pos];
pos++;
}
//
// Reading the header value (which can be spanned over multiple lines)
//
boolean eol = false;
boolean validLine = true;
boolean space = false;
while (space && pos < len) {
if ((buf[pos] == Constants.SP) || (buf[pos] == Constants.HT)) {
pos++;
} else {
space = false;
}
}
// Mark the current buffer position
start = pos;
int realPos = pos;
while (validLine) {
// space = true;
// // Skipping spaces
// while (space && pos < len) {
// if ((buf[pos] == Constants.SP) || (buf[pos] == Constants.HT)) {
// pos++;
// } else {
// space = false;
// }
//
// }
int lastSignificantChar = realPos;
// Reading bytes until the end of the line
while (!eol && pos < len) {
if (buf[pos] == Constants.CR) {
} else if (buf[pos] == Constants.LF) {
eol = true;
} else if (buf[pos] == Constants.SP) {
realPos++;
} else {
realPos++;
// lastSignificantChar = realPos;
}
pos++;
}
//realPos = lastSignificantChar;
// Checking the first character of the new line. If the character
// is a LWS, then it's a multiline header
// Read new bytes if needed
chr = buf[pos];
if ((chr != Constants.SP) && (chr != Constants.HT)) {
validLine = false;
} else {
eol = false;
realPos++;
}
}
// Set the header value
// if(headerValue != null)
// headerValue.setBytes(buf, start, realPos - start);
if(fieldName != null){
fieldValue = new String(buf, start, realPos - start).trim();
if(fieldName.equalsIgnoreCase("Content-Type")){
response.setContentType(fieldValue);
}else if(fieldName.equalsIgnoreCase("status")){
int value = 200;
String msg = "OK";
if(fieldValue != null){
int index = fieldValue.indexOf(' ');
if(index != -1){
try{
value = Integer.valueOf(fieldValue.substring(0, index));
}catch(NumberFormatException e){
//ignore, defaults to 200
}
msg = fieldValue.substring(index, fieldValue.length()).trim();
}
}
response.setStatus(value, msg);
}else if(fieldName.equalsIgnoreCase("Content-Length")){
try{
response.setContentLength(Integer.valueOf(fieldValue));
}catch(NumberFormatException e){
//let grizzly write the length from the response body
}
}else{
response.addHeader(fieldName, fieldValue);
}
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy