use of sun.net.www.HeaderParser in project jdk8u_jdk by JetBrains.
the class HeaderTests method checkHeader.
static void checkHeader(Iterator iter, String[][][] expect) {
for (int i = 0; iter.hasNext(); ) {
String s = (String) iter.next();
HeaderParser p = new HeaderParser(s);
boolean empty = check(p, expect[i]);
if (!empty) {
i++;
}
}
}
use of sun.net.www.HeaderParser in project jdk8u_jdk by JetBrains.
the class HttpClient method parseHTTPHeader.
private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, HttpURLConnection httpuc) throws IOException {
/* If "HTTP/*" is found in the beginning, return true. Let
* HttpURLConnection parse the mime header itself.
*
* If this isn't valid HTTP, then we don't try to parse a header
* out of the beginning of the response into the responses,
* and instead just queue up the output stream to it's very beginning.
* This seems most reasonable, and is what the NN browser does.
*/
keepAliveConnections = -1;
keepAliveTimeout = 0;
boolean ret = false;
byte[] b = new byte[8];
try {
int nread = 0;
serverInput.mark(10);
while (nread < 8) {
int r = serverInput.read(b, nread, 8 - nread);
if (r < 0) {
break;
}
nread += r;
}
String keep = null;
ret = b[0] == 'H' && b[1] == 'T' && b[2] == 'T' && b[3] == 'P' && b[4] == '/' && b[5] == '1' && b[6] == '.';
serverInput.reset();
if (ret) {
// is valid HTTP - response started w/ "HTTP/1."
responses.parseHeader(serverInput);
// we've finished parsing http headers
// check if there are any applicable cookies to set (in cache)
CookieHandler cookieHandler = httpuc.getCookieHandler();
if (cookieHandler != null) {
URI uri = ParseUtil.toURI(url);
// it is resolved.
if (uri != null)
cookieHandler.put(uri, responses.getHeaders());
}
/* decide if we're keeping alive:
* This is a bit tricky. There's a spec, but most current
* servers (10/1/96) that support this differ in dialects.
* If the server/client misunderstand each other, the
* protocol should fall back onto HTTP/1.0, no keep-alive.
*/
if (usingProxy) {
// not likely a proxy will return this
keep = responses.findValue("Proxy-Connection");
}
if (keep == null) {
keep = responses.findValue("Connection");
}
if (keep != null && keep.toLowerCase(Locale.US).equals("keep-alive")) {
/* some servers, notably Apache1.1, send something like:
* "Keep-Alive: timeout=15, max=1" which we should respect.
*/
HeaderParser p = new HeaderParser(responses.findValue("Keep-Alive"));
if (p != null) {
/* default should be larger in case of proxy */
keepAliveConnections = p.findInt("max", usingProxy ? 50 : 5);
keepAliveTimeout = p.findInt("timeout", usingProxy ? 60 : 5);
}
} else if (b[7] != '0') {
/*
* We're talking 1.1 or later. Keep persistent until
* the server says to close.
*/
if (keep != null) {
/*
* The only Connection token we understand is close.
* Paranoia: if there is any Connection header then
* treat as non-persistent.
*/
keepAliveConnections = 1;
} else {
keepAliveConnections = 5;
}
}
} else if (nread != 8) {
if (!failedOnce && requests != null) {
failedOnce = true;
if (getRequestMethod().equals("CONNECT") || streaming || (httpuc.getRequestMethod().equals("POST") && !retryPostProp)) {
// do not retry the request
} else {
closeServer();
cachedHttpClient = false;
openServer();
if (needsTunneling()) {
MessageHeader origRequests = requests;
httpuc.doTunneling();
requests = origRequests;
}
afterConnect();
writeRequests(requests, poster);
return parseHTTP(responses, pi, httpuc);
}
}
throw new SocketException("Unexpected end of file from server");
} else {
// we can't vouche for what this is....
responses.set("Content-type", "unknown/unknown");
}
} catch (IOException e) {
throw e;
}
int code = -1;
try {
String resp;
resp = responses.getValue(0);
/* should have no leading/trailing LWS
* expedite the typical case by assuming it has
* form "HTTP/1.x <WS> 2XX <mumble>"
*/
int ind;
ind = resp.indexOf(' ');
while (resp.charAt(ind) == ' ') ind++;
code = Integer.parseInt(resp.substring(ind, ind + 3));
} catch (Exception e) {
}
if (code == HTTP_CONTINUE && ignoreContinue) {
responses.reset();
return parseHTTPHeader(responses, pi, httpuc);
}
long cl = -1;
/*
* Set things up to parse the entity body of the reply.
* We should be smarter about avoid pointless work when
* the HTTP method and response code indicate there will be
* no entity body to parse.
*/
String te = responses.findValue("Transfer-Encoding");
if (te != null && te.equalsIgnoreCase("chunked")) {
serverInput = new ChunkedInputStream(serverInput, this, responses);
/*
* If keep alive not specified then close after the stream
* has completed.
*/
if (keepAliveConnections <= 1) {
keepAliveConnections = 1;
keepingAlive = false;
} else {
keepingAlive = true;
}
failedOnce = false;
} else {
/*
* If it's a keep alive connection then we will keep
* (alive if :-
* 1. content-length is specified, or
* 2. "Not-Modified" or "No-Content" responses - RFC 2616 states that
* 204 or 304 response must not include a message body.
*/
String cls = responses.findValue("content-length");
if (cls != null) {
try {
cl = Long.parseLong(cls);
} catch (NumberFormatException e) {
cl = -1;
}
}
String requestLine = requests.getKey(0);
if ((requestLine != null && (requestLine.startsWith("HEAD"))) || code == HttpURLConnection.HTTP_NOT_MODIFIED || code == HttpURLConnection.HTTP_NO_CONTENT) {
cl = 0;
}
if (keepAliveConnections > 1 && (cl >= 0 || code == HttpURLConnection.HTTP_NOT_MODIFIED || code == HttpURLConnection.HTTP_NO_CONTENT)) {
keepingAlive = true;
failedOnce = false;
} else if (keepingAlive) {
/* Previously we were keeping alive, and now we're not. Remove
* this from the cache (but only here, once) - otherwise we get
* multiple removes and the cache count gets messed up.
*/
keepingAlive = false;
}
}
if (cl > 0) {
if (pi != null) {
// Progress monitor is enabled
pi.setContentType(responses.findValue("content-type"));
}
if (isKeepingAlive()) {
// Wrap KeepAliveStream if keep alive is enabled.
logFinest("KeepAlive stream used: " + url);
serverInput = new KeepAliveStream(serverInput, pi, cl, this);
failedOnce = false;
} else {
serverInput = new MeteredStream(serverInput, pi, cl);
}
} else if (cl == -1) {
if (pi != null) {
// Progress monitoring is enabled.
pi.setContentType(responses.findValue("content-type"));
// Wrap MeteredStream for tracking indeterministic
// progress, even if the input stream is ChunkedInputStream.
serverInput = new MeteredStream(serverInput, pi, cl);
} else {
// Progress monitoring is disabled, and there is no
// need to wrap an unknown length input stream.
// ** This is an no-op **
}
} else {
if (pi != null)
pi.finishTracking();
}
return ret;
}
use of sun.net.www.HeaderParser in project jdk8u_jdk by JetBrains.
the class DigestAuthentication method checkResponse.
public void checkResponse(String header, String method, String uri) throws IOException {
char[] passwd = pw.getPassword();
String username = pw.getUserName();
boolean qop = params.authQop();
String opaque = params.getOpaque();
String cnonce = params.cnonce;
String nonce = params.getNonce();
String algorithm = params.getAlgorithm();
int nccount = params.getNCCount();
String ncstring = null;
if (header == null) {
throw new ProtocolException("No authentication information in response");
}
if (nccount != -1) {
ncstring = Integer.toHexString(nccount).toUpperCase();
int len = ncstring.length();
if (len < 8)
ncstring = zeroPad[len] + ncstring;
}
try {
String expected = computeDigest(false, username, passwd, realm, method, uri, nonce, cnonce, ncstring);
HeaderParser p = new HeaderParser(header);
String rspauth = p.findValue("rspauth");
if (rspauth == null) {
throw new ProtocolException("No digest in response");
}
if (!rspauth.equals(expected)) {
throw new ProtocolException("Response digest invalid");
}
/* Check if there is a nextnonce field */
String nextnonce = p.findValue("nextnonce");
if (nextnonce != null && !"".equals(nextnonce)) {
params.setNonce(nextnonce);
}
} catch (NoSuchAlgorithmException ex) {
throw new ProtocolException("Unsupported algorithm in response");
}
}
use of sun.net.www.HeaderParser in project jdk8u_jdk by JetBrains.
the class DigestAuthentication method isAuthorizationStale.
/**
* Check if the header indicates that the current auth. parameters are stale.
* If so, then replace the relevant field with the new value
* and return true. Otherwise return false.
* returning true means the request can be retried with the same userid/password
* returning false means we have to go back to the user to ask for a new
* username password.
*/
@Override
public boolean isAuthorizationStale(String header) {
HeaderParser p = new HeaderParser(header);
String s = p.findValue("stale");
if (s == null || !s.equals("true"))
return false;
String newNonce = p.findValue("nonce");
if (newNonce == null || "".equals(newNonce)) {
return false;
}
params.setNonce(newNonce);
return true;
}
use of sun.net.www.HeaderParser in project jdk8u_jdk by JetBrains.
the class EmbeddedEquals method main.
public static void main(String[] args) {
HeaderParser hp = new HeaderParser(test);
String r1 = hp.findValue("nonce");
if (r1 == null || !r1.equals("Ovqrpw==b20ff3b0ea3f3a18f1d6293331edaafdb98f5bef")) {
throw new RuntimeException("first findValue returned wrong result: " + r1);
}
r1 = hp.findValue("AlGoRiThm");
if (r1 == null || !r1.equals("MD5")) {
throw new RuntimeException("second findValue returned wrong result: " + r1);
}
}
Aggregations