View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2013
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag       Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   * 12917      2004-11-11  thschaef     HTTP Header parsing does not work for UNIX line delimitor
19   * 1535756    2006-08-07  lupusalex    Make code warning free
20   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
21   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
22   * 2204488 	  2008-10-28  raman_arora  Fix code to remove compiler warnings
23   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
24   * 2750520    2009-04-10  blaschke-oss Code cleanup from empty statement et al
25   * 3557283    2012-11-05  blaschke-oss Print full response when get EOF from CIMOM
26   *    2709    2013-11-13  blaschke-oss Lower the level of the EOF message to FINE
27   */
28  
29  package org.metricshub.wbem.sblim.cimclient.internal.http;
30  
31  /*-
32   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
33   * WBEM Java Client
34   * ჻჻჻჻჻჻
35   * Copyright 2023 - 2025 MetricsHub
36   * ჻჻჻჻჻჻
37   * Licensed under the Apache License, Version 2.0 (the "License");
38   * you may not use this file except in compliance with the License.
39   * You may obtain a copy of the License at
40   *
41   *      http://www.apache.org/licenses/LICENSE-2.0
42   *
43   * Unless required by applicable law or agreed to in writing, software
44   * distributed under the License is distributed on an "AS IS" BASIS,
45   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46   * See the License for the specific language governing permissions and
47   * limitations under the License.
48   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49   */
50  
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.util.logging.Level;
54  import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
55  
56  /**
57   * Class HttpMethod provides a method to read a line from a given input stream
58   *
59   */
60  public abstract class HttpMethod {
61  
62  	/**
63  	 * Reads a line from a given input stream
64  	 *
65  	 * @param pStream
66  	 *            The input stream
67  	 * @return The line
68  	 * @throws IOException
69  	 */
70  	public static String readLine(InputStream pStream) throws IOException {
71  		if (pStream == null) return null;
72  
73  		char buffer[] = new char[16];
74  		int used = 0;
75  		int prevChar;
76  		boolean flag = true;
77  
78  		for (; (prevChar = pStream.read()) >= 0; buffer[used++] = (char) prevChar) {
79  			flag = false;
80  
81  			// @12917 - thschaef
82  			// see http://www.w3.org/Protocols/HTTP/AsImplemented.html for
83  			// details
84  			// Lines shall be delimited by an optional carriage return followed
85  			// by a mandatory line feed character.
86  			// The client should not assume that the carriage return will be
87  			// present. Lines may be of any length.
88  			// Well-behaved servers should restrict line length to 80 characters
89  			// excluding the CR LF pair.
90  
91  			// if (prevChar == 13) continue;
92  			if (prevChar == 10) break;
93  
94  			if (used >= buffer.length) {
95  				char tmp[] = new char[buffer.length << 1];
96  				System.arraycopy(buffer, 0, tmp, 0, used);
97  				buffer = tmp;
98  			}
99  		}
100 		if (flag) {
101 			LogAndTraceBroker
102 				.getBroker()
103 				.trace(
104 					Level.FINE,
105 					"Unexpected EOF trying to read line from input stream - CIMOM closed its end of socket, check it for connection issues"
106 				);
107 			throw new IOException("Unexpected EOF");
108 		}
109 
110 		for (; used > 0 && buffer[used - 1] <= ' '; used--) {
111 			// back up over blanks and non-printables at end of line
112 		}
113 
114 		return String.copyValueOf(buffer, 0, used);
115 	}
116 }