source: branches/streams/abcl/src/org/armedbear/lisp/util/HttpHead.java

Last change on this file was 14627, checked in by Mark Evenson, 11 years ago

(partially) restore CL:LOAD from jar files.

There is apparently a fair amount of "breakage" of cases that used to
load no longer working which seems due to the changes in the semantics
for finding the FASL init loader. The following tests are now broken
but no longer cause the JVM to crash: JAR-PATHNAME.LOAD.HTTP.1,
JAR-PATHNAME.LOAD.HTTP.2, JAR-PATHNAME.LOAD.HTTP.4,
JAR-PATHNAME.LOAD.HTTP.6, JAR-PATHNAME.LOAD.HTTP.7,
and JAR-PATHNAME.LOAD.HTTP.9. Need to follow this up in subsequent work.

Fixed the underlying HttpHead?.get() interface used to determine
whether to used a cache version. The custom HTTP HEAD code that was
working under Java 6 no longer worked on Java 7.

Added to HttpHead?.get() asynchronous java.lang.Throwable on a socket
timeout of 5000 ms.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/*
2 * HttpHead.java
3 *
4 * Copyright (C) 2010 Mark Evenson
5 * $Id: HttpHead.java 14627 2014-02-16 21:25:20Z mevenson $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33package org.armedbear.lisp.util;
34
35import org.armedbear.lisp.Debug;
36
37import java.io.BufferedReader;
38import java.io.IOException;
39import java.io.InputStreamReader;
40import java.io.OutputStreamWriter;
41import java.io.PrintWriter;
42import java.net.InetSocketAddress;
43import java.net.MalformedURLException;
44import java.net.Proxy;
45import java.net.Socket;
46import java.net.URL;
47
48/**
49 * Use HTTP/1.1 HEAD to retrieve the specified header field.
50 */
51public class HttpHead {
52    static private String get(String urlString, String key) throws IOException {
53        URL url = null;
54        try {
55            url = new URL(urlString);
56        } catch (MalformedURLException e) {
57            log("Failed to form url from " + "'" + urlString + "'" + ": " + e);
58        }
59        return get(url, key);
60    }
61
62    static public String get(URL url, String key) throws IOException {
63        Socket socket = null;
64        String result = null;
65        try {
66            String protocol = url.getProtocol();
67            if (!protocol.equals("http")) {
68                log("The protocol " + "'" + protocol + "'" + " is not http.");
69                return result;
70            }
71            String host = url.getHost();
72            int port = url.getPort();
73            if (port == -1) {
74                port = 80;
75            }
76            socket = new Socket(host, port);
77           
78            PrintWriter out = null;
79            BufferedReader in = null;
80            try {
81                socket.setSoTimeout(5000); // ms
82                out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
83                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
84            } catch (IOException e) {
85                log("Failed to establish socket io: " + e);
86                return result;
87            }
88
89            String CRLF = "\r\n";
90            String head = "HEAD " + url.getPath() + " HTTP/1.1";
91            out.print(head + CRLF);
92            out.print("Host: " + url.getAuthority() + CRLF);
93            out.print("Connection: close" + CRLF);
94            out.print(CRLF);
95            out.flush();
96
97            String line = null;
98            try {
99                line = in.readLine();
100            } catch (IOException e) {
101                log("Failed to read HTTP response: " + e);
102            }
103            if (line == null) {
104              throw new IOException("Could not access URL to parse headers."); 
105            }
106            String status[] = line.split("\\s");
107            if (status[1].equals("200")) {
108                result = findHeader(in, key);
109            } else if (status[1].startsWith("3")) {
110                // Follow redirects ad nauseum
111                String location = findHeader(in, "Location");
112                if (location != null) {
113                    return get(location, key);
114                }
115            } else {
116                log("Unexpected response: " + line);
117            }
118        } finally {
119            try {
120                socket.close();
121            } catch (IOException e) {
122            }
123        }
124        return result;
125    }
126
127    static private String findHeader(BufferedReader in, String key) {
128        String result = null;
129        String line;
130        try {
131            while ((line = in.readLine()) != null) {
132                int i = line.indexOf(":");
133                if (i == -1) {
134                    continue; // XXX parse multi-line HTTP headers
135                }
136                String k = line.substring(0, i);
137                String v = line.substring(i + 1).trim();
138                if (k.equals(key)) {
139                    result = v;
140                    break;
141                }
142            }
143        } catch (IOException e) {
144            log("Failed to read headers: " + e);
145        }
146        return result;
147    }
148
149    static private void log(String message) {
150        Debug.warn(message);
151    }
152
153    public static void main(String argv[]) {
154      if (argv.length != 1) {
155        System.out.println("Usage: <cmd> URL");
156        return;
157      }
158      String modified = null;
159      try {
160        modified = get(argv[0], "Last-Modified");
161      } catch (IOException ex) {
162        System.err.println("Unable to get Last-Modified header: ");
163        ex.printStackTrace(System.err);
164      }
165      if (modified != null) {
166        System.out.println("Last-Modified: " + modified);
167      } else {
168        System.out.println("No result returned.");
169      }
170    }
171}
Note: See TracBrowser for help on using the repository browser.