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

Last change on this file was 12656, checked in by Mark Evenson, 15 years ago

Properly implement HTTP/1.1 HEAD requests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/*
2 * HttpHead.java
3 *
4 * Copyright (C) 2010 Mark Evenson
5 * $Id: HttpHead.java 12656 2010-05-06 20:15:26Z 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.PrintWriter;
41import java.net.InetSocketAddress;
42import java.net.MalformedURLException;
43import java.net.Proxy;
44import java.net.Socket;
45import java.net.URL;
46
47/**
48 * Use HTTP/1.1 HEAD to retrieve the specified header field.
49 */
50public class HttpHead {
51    static private String get(String urlString, String key) {
52        URL url = null;
53        try {
54            url = new URL(urlString);
55        } catch (MalformedURLException e) {
56            log("Failed to form url from " + "'" + urlString + "'" + ": " + e);
57        }
58        return get(url, key);
59    }
60
61    static public String get(URL url, String key) {
62        Socket socket = null;
63        String result = null;
64        try {
65            String protocol = url.getProtocol();
66            if (!protocol.equals("http")) {
67                log("The protocol " + "'" + protocol + "'" + " is not http.");
68                return result;
69            }
70
71            socket = new Socket(Proxy.NO_PROXY); // XXX add Proxy
72
73            int port = url.getPort();
74            if (port == -1) {
75                port = 80;
76            }
77            InetSocketAddress address = new InetSocketAddress(url.getHost(), port);
78            try {
79                socket.connect(address, 5000); // ??? too long?  too short?
80            } catch (IOException ex) {
81                log("Connection failed: " + ex);
82                return result;
83            }
84
85            PrintWriter out = null;
86            BufferedReader in = null;
87            try {
88                out = new PrintWriter(socket.getOutputStream());
89                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
90            } catch (IOException e) {
91                log("Failed to establish socket io: " + e);
92                return result;
93            }
94
95            String head = "HEAD " + url.getPath() + " HTTP/1.1";
96            out.println(head);
97            out.println("Host: " + url.getAuthority());
98            out.println("Connection: close");
99            out.println("");
100            out.flush();
101
102            String line = null;
103            try {
104                line = in.readLine();
105            } catch (IOException e) {
106                log("Failed to read HTTP response: " + e);
107            }
108            String status[] = line.split("\\s");
109            if (status[1].equals("200")) {
110                result = findHeader(in, key);
111            } else if (status[1].startsWith("3")) {
112                // Follow redirects ad nauseum
113                String location = findHeader(in, "Location");
114                if (location != null) {
115                    return get(location, key);
116                }
117            } else {
118                log("Unexpected response: " + line);
119            }
120        } finally {
121            try {
122                socket.close();
123            } catch (IOException e) {
124            }
125        }
126        return result;
127    }
128
129    static private String findHeader(BufferedReader in, String key) {
130        String result = null;
131        String line;
132        try {
133            while ((line = in.readLine()) != null) {
134                int i = line.indexOf(":");
135                if (i == -1) {
136                    continue; // XXX parse multi-line HTTP headers
137                }
138                String k = line.substring(0, i);
139                String v = line.substring(i + 1).trim();
140                if (k.equals(key)) {
141                    result = v;
142                    break;
143                }
144            }
145        } catch (IOException e) {
146            log("Failed to read headers: " + e);
147        }
148        return result;
149    }
150
151    static private void log(String message) {
152        Debug.warn(message);
153    }
154
155    public static void main(String argv[]) {
156        if (argv.length != 1) {
157            System.out.println("Usage: <cmd> URL");
158            return;
159        }
160        String modified = get(argv[0], "Last-Modified");
161        if (modified != null) {
162            System.out.println("Last-Modified: " + modified);
163        } else {
164            System.out.println("No result returned.");
165        }
166    }
167}
Note: See TracBrowser for help on using the repository browser.