/** * @param request * @return * @throws IOException */ public InputStream sendRequest(byte[] request) throws IOException { output.write(("POST " + uri + " HTTP/1.0\r\n").getBytes()); output.write(("User-Agent: " + XmlRpc.version + "\r\n").getBytes()); output.write(("Host: " + host + "\r\n").getBytes()); if (XmlRpc.getKeepAlive()) { output.write("Connection: Keep-Alive\r\n".getBytes()); } output.write("Content-Type: text/xml\r\n".getBytes()); if (auth != null) { output.write(("Authorization: Basic " + auth + "\r\n").getBytes()); } output.write(("Content-Length: " + request.length).getBytes()); output.write("\r\n\r\n".getBytes()); output.write(request); output.flush(); // start reading server response headers String line = readLine(); if (XmlRpc.debug) { System.out.println(line); } int contentLength = -1; try { StringTokenizer tokens = new StringTokenizer(line); final String httpversion = tokens.nextToken(); String statusCode = tokens.nextToken(); String statusMsg = tokens.nextToken("\n\r"); keepalive = XmlRpc.getKeepAlive() && "HTTP/1.1".equals(httpversion); if (!"200".equals(statusCode)) { throw new IOException("Unexpected Response from Server: " + statusMsg); } } catch (IOException iox) { throw iox; } catch (Exception x) { // x.printStackTrace (); throw new IOException("Server returned invalid Response."); } do { line = readLine(); if (line != null) { if (XmlRpc.debug) { System.out.println(line); } line = line.toLowerCase(); if (line.startsWith("content-length:")) { contentLength = Integer.parseInt(line.substring(15).trim()); } if (line.startsWith("connection:")) { keepalive = XmlRpc.getKeepAlive() && line.indexOf("keep-alive") > -1; } } } while (line != null && !line.equals("")); return new ServerInputStream(input, contentLength); }
public void run() { try { boolean keepalive = false; do { // reset user authentication user = password = null; String line = readLine(); // Netscape sends an extra \n\r after bodypart, swallow it if ("".equals(line)) line = readLine(); if (XmlRpc.debug) System.err.println(line); // get time of last request lastRequest = System.currentTimeMillis(); int contentLength = -1; // tokenize first line of HTTP request StringTokenizer tokens = new StringTokenizer(line); String method = tokens.nextToken(); String uri = tokens.nextToken(); String httpversion = tokens.nextToken(); keepalive = XmlRpc.getKeepAlive() && "HTTP/1.1".equals(httpversion); do { line = readLine(); if (line != null) { if (XmlRpc.debug) System.err.println(line); String lineLower = line.toLowerCase(); if (lineLower.startsWith("content-length:")) contentLength = Integer.parseInt(line.substring(15).trim()); if (lineLower.startsWith("connection:")) keepalive = XmlRpc.getKeepAlive() && lineLower.indexOf("keep-alive") > -1; if (lineLower.startsWith("authorization: basic ")) parseAuth(line); } } while (line != null && !line.equals("")); if ("POST".equalsIgnoreCase(method)) { ServerInputStream sin = new ServerInputStream(input, contentLength); byte result[] = xmlrpc.execute(sin, user, password); output.write(httpversion.getBytes()); output.write(ok); output.write(server); if (keepalive) output.write(conkeep); else output.write(conclose); output.write(ctype); output.write(clength); output.write(Integer.toString(result.length).getBytes()); output.write(doubleNewline); output.write(result); output.flush(); } else { output.write(httpversion.getBytes()); output.write(" 400 Bad Request\r\n".getBytes()); output.write("Server: helma.XML-RPC\r\n\r\n".getBytes()); output.write(("Method " + method + " not implemented (try POST)").getBytes()); output.flush(); keepalive = false; } } while (keepalive); } catch (Exception exception) { if (XmlRpc.debug) { System.err.println(exception); exception.printStackTrace(); } } finally { try { socket.close(); } catch (IOException ignore) { } } }
public void run() { try { boolean keepAlive = false; do { // reset user authentication user = null; password = null; String line = readLine(); // Netscape sends an extra \n\r after bodypart, swallow it if (line != null && line.length() == 0) { line = readLine(); } if (XmlRpc.debug) { System.out.println(line); } int contentLength = -1; // tokenize first line of HTTP request StringTokenizer tokens = new StringTokenizer(line); String method = tokens.nextToken(); String uri = tokens.nextToken(); String httpVersion = tokens.nextToken(); keepAlive = XmlRpc.getKeepAlive() && HTTP_11.equals(httpVersion); do { line = readLine(); if (line != null) { if (XmlRpc.debug) { System.out.println(line); } String lineLower = line.toLowerCase(); if (lineLower.startsWith("content-length:")) { contentLength = Integer.parseInt(line.substring(15).trim()); } if (lineLower.startsWith("connection:")) { keepAlive = XmlRpc.getKeepAlive() && lineLower.indexOf("keep-alive") > -1; } if (lineLower.startsWith("authorization: basic ")) { parseAuth(line); } } } while (line != null && line.length() != 0); if ("POST".equalsIgnoreCase(method)) { ServerInputStream sin = new ServerInputStream(input, contentLength); try { byte[] result = xmlrpc.execute(sin, user, password); writeResponse(result, httpVersion, keepAlive); } catch (AuthenticationFailed unauthorized) { keepAlive = false; writeUnauthorized(httpVersion, method); } } else { keepAlive = false; writeBadRequest(httpVersion, method); } output.flush(); } while (keepAlive); } catch (Exception exception) { if (XmlRpc.debug) { exception.printStackTrace(); } else { System.err.println(exception); } } finally { try { if (socket != null) { socket.close(); } } catch (IOException ignore) { } } }