1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java $ 3 * $Revision: 617638 $ 4 * $Date: 2008-02-01 12:49:26 -0800 (Fri, 01 Feb 2008) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32 package org.apache.http.impl.conn; 33 34 import java.io.IOException; 35 36 import org.apache.http.HttpException; 37 import org.apache.http.HttpMessage; 38 import org.apache.http.HttpResponseFactory; 39 import org.apache.http.NoHttpResponseException; 40 import org.apache.http.ProtocolException; 41 import org.apache.http.StatusLine; 42 import org.apache.http.conn.params.ConnConnectionPNames; 43 import org.apache.http.impl.io.AbstractMessageParser; 44 import org.apache.http.io.SessionInputBuffer; 45 import org.apache.http.message.LineParser; 46 import org.apache.http.message.ParserCursor; 47 import org.apache.http.params.HttpParams; 48 import org.apache.http.util.CharArrayBuffer; 49 50 public class DefaultResponseParser extends AbstractMessageParser { 51 52 private final HttpResponseFactory responseFactory; 53 private final CharArrayBuffer lineBuf; 54 private final int maxGarbageLines; 55 56 public DefaultResponseParser( 57 final SessionInputBuffer buffer, 58 final LineParser parser, 59 final HttpResponseFactory responseFactory, 60 final HttpParams params) { 61 super(buffer, parser, params); 62 if (responseFactory == null) { 63 throw new IllegalArgumentException 64 ("Response factory may not be null"); 65 } 66 this.responseFactory = responseFactory; 67 this.lineBuf = new CharArrayBuffer(128); 68 this.maxGarbageLines = params.getIntParameter( 69 ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE); 70 } 71 72 73 @Override 74 protected HttpMessage parseHead( 75 final SessionInputBuffer sessionBuffer) throws IOException, HttpException { 76 // clear the buffer 77 this.lineBuf.clear(); 78 //read out the HTTP status string 79 int count = 0; 80 ParserCursor cursor = null; 81 do { 82 int i = sessionBuffer.readLine(this.lineBuf); 83 if (i == -1 && count == 0) { 84 // The server just dropped connection on us 85 throw new NoHttpResponseException("The target server failed to respond"); 86 } 87 cursor = new ParserCursor(0, this.lineBuf.length()); 88 if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) { 89 // Got one 90 break; 91 } else if (i == -1 || count >= this.maxGarbageLines) { 92 // Giving up 93 throw new ProtocolException("The server failed to respond with a " + 94 "valid HTTP response"); 95 } 96 count++; 97 } while(true); 98 //create the status line from the status string 99 StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor); 100 return this.responseFactory.newHttpResponse(statusline, null); 101 } 102 103 } 104