1 31 32 package org.apache.commons.httpclient; 33 34 58 public class StatusLine { 59 60 62 63 private final String statusLine; 64 65 66 private final String httpVersion; 67 68 69 private final int statusCode; 70 71 72 private final String reasonPhrase; 73 74 75 77 83 public StatusLine(final String statusLine) 84 throws HttpException { 85 86 int length = statusLine.length(); 87 88 int at = 0; 89 int start = 0; 90 try { 91 while (Character.isWhitespace(statusLine.charAt(at))) { 92 ++at; 93 ++start; 94 } 95 if (!"HTTP".equals(statusLine.substring(at, at += 4))) { 96 throw new HttpException("Status-Line '" + statusLine 97 + "' does not start with HTTP"); 98 } 99 at = statusLine.indexOf(" ", at); 101 if (at <= 0) { 102 throw new HttpException( 103 "Unable to parse HTTP-Version from the status line: '" 104 + statusLine + "'"); 105 } 106 this.httpVersion = (statusLine.substring(start, at)).toUpperCase(); 107 while (statusLine.charAt(at) == ' ') { 109 at++; 110 } 111 int to = statusLine.indexOf(" ", at); 113 if (to < 0) { 114 to = length; 115 } 116 try { 117 this.statusCode = Integer.parseInt(statusLine.substring(at, to)); 118 } catch (NumberFormatException e) { 119 throw new HttpException( 120 "Unable to parse status code from status line: '" 121 + statusLine + "'"); 122 } 123 at = to + 1; 125 if (at < length) { 126 this.reasonPhrase = statusLine.substring(at).trim(); 127 } else { 128 this.reasonPhrase = ""; 129 } 130 } catch (StringIndexOutOfBoundsException e) { 131 throw new HttpException("Status-Line '" + statusLine + "' is not valid"); 132 } 133 this.statusLine = new String (statusLine); 135 } 136 137 138 140 143 public final int getStatusCode() { 144 return statusCode; 145 } 146 147 150 public final String getHttpVersion() { 151 return httpVersion; 152 } 153 154 157 public final String getReasonPhrase() { 158 return reasonPhrase; 159 } 160 161 165 public final String toString() { 166 return statusLine; 167 } 168 169 175 public static boolean startsWithHTTP(final String s) { 176 try { 177 int at = 0; 178 while (Character.isWhitespace(s.charAt(at))) { 179 ++at; 180 } 181 return ("HTTP".equals(s.substring(at, at + 4))); 182 } catch (StringIndexOutOfBoundsException e) { 183 return false; 184 } 185 } 186 } 187 | Popular Tags |