summaryrefslogtreecommitdiff
path: root/source/de/anomic/http/HttpClient.java
blob: 656aa8d60326d819e840d963de2c2097d48fd60c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// HttpClient.java
// (C) 2008 by Daniel Raap; danielr@users.berlios.de
// first published 2.4.2008 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2008-03-14 01:16:04 +0100 (Fr, 14 Mrz 2008) $
// $LastChangedRevision: 4558 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.http;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import de.anomic.server.logging.serverLog;

/**
 * Client who does http requests
 * 
 * some methods must be implemented (the "socket-layer")
 */
public abstract class HttpClient {
    private static final String systemOST;
    static {
        // provide system information for client identification
        String loc = generateLocation();
        systemOST =
            System.getProperty("os.arch", "no-os-arch") + " " +
            System.getProperty("os.name", "no-os-name") + " " +
            System.getProperty("os.version", "no-os-version") + "; " +
            "java " + System.getProperty("java.version", "no-java-version") + "; " + loc;
    }

    /**
     * generating the location string
     * 
     * @return
     */
    public static String generateLocation() {
        String loc = System.getProperty("user.timezone", "nowhere");
        final int p = loc.indexOf("/");
        if (p > 0) loc = loc.substring(0,p);
        loc = loc + "/" + System.getProperty("user.language", "dumb");
        return loc;
    }

    /**
     * @return the systemOST
     */
    public static String getSystemOST() {
        return systemOST;
    }

    /**
     * "This method can be used for obtaining metainformation about the entity implied by the request without
     * transferring the entity-body itself" [RFC 2616, sect. 9.4]
     * 
     * @param uri
     * @return response header
     * @throws IOException if data cannot be read
     */
    public abstract HttpResponse HEAD(String uri) throws IOException;

    /**
     * "The GET method means retrieve whatever information [...] is identified by the Request-URI" [RFC 2616, sect. 9.3]
     * 
     * @param uri
     * @return response body
     * @throws IOException if data cannot be read
     */
    public abstract HttpResponse GET(String uri) throws IOException;

    /**
     * "Providing a block of data [...] to a data-handling process;" [RFC 2616, sect. 9.5]
     * 
     * @param uri
     * @param nameDataPairs
     * @return response body
     * @throws IOException if data cannot be read
     */
    public abstract HttpResponse POST(String uri, Map<String, ?> nameDataPairs) throws IOException;

    /**
     * "for use with a proxy that can dynamically switch to being a tunnel" [RFC 2616, sect. 9.9]
     * 
     * @param host
     * @param port
     * @return
     * @throws IOException
     */
    public abstract HttpResponse CONNECT(String host, int port) throws IOException;

    /**
     * use proxyConfig to establish the connection
     * 
     * @param proxyConfig
     */
    public abstract void setProxy(httpRemoteProxyConfig proxyConfig);

    /**
     * sets the header for all coming requests
     * 
     * @param header may be null
     */
    public abstract void setHeader(httpHeader header);

    /**
     * sets the timeout in milliseconds
     * 
     * @param timeout
     */
    public abstract void setTimeout(int timeout);

    /**
     * gives the user-agent used by this http-client
     * 
     * @return
     */
    public abstract String getUserAgent();
    
    /**
     * Returns the given date in an HTTP-usable format. (according to RFC1123/RFC822)
     * 
     * @param date The Date-Object to be converted.
     * @return String with the date.
     */
    public abstract String date2String(Date date);
    
    /**
     * for easy access
     * @see date2String(Date)
     * @param date
     * @return
     */
    public static String dateString(Date date) {
        return HttpFactory.newClient().date2String(date);
    }

    /**
     * Gets a page (as raw bytes)
     * 
     * @param uri
     * @return
     */
    public static byte[] wget(final String uri) {
        return wget(uri, null, null);
    }

    /**
     * Gets a page (as raw bytes) addressing vhost at host in uri
     * 
     * @param uri
     * @param vhost used if host in uri cannot be resolved (yacy tld)
     * @return
     */
    public static byte[] wget(final String uri, final String vhost) {
        return wget(uri, null, vhost);
    }

    /**
     * Gets a page (as raw bytes) with specified header
     * 
     * @param uri
     * @param header
     * @return
     */
    public static byte[] wget(final String uri, final httpHeader header) {
        return wget(uri, header, null);
    }

    /**
     * Gets a page (as raw bytes) addressing vhost at host in uri with specified header
     * 
     * @param uri
     * @param header
     * @param vhost
     * @return
     * @assert uri != null
     */
    public static byte[] wget(final String uri, httpHeader header, final String vhost) {
        assert uri != null : "precondition violated: uri != null";
        final HttpClient client = HttpFactory.newClient();
    
        // set header
        header = addHostHeader(header, vhost);
        client.setHeader(header);
    
        // do the request
        try {
            final HttpResponse response = client.GET(uri);
            return response.getData();
        } catch (final IOException e) {
            serverLog.logWarning("HTTPC", "wget(" + uri + ") failed: " + e.getMessage());
        }
        return null;
    }

    /**
     * adds a Host-header to the header if vhost is not null
     * 
     * @param header
     * @param vhost
     * @return
     */
    private static httpHeader addHostHeader(httpHeader header, final String vhost) {
        if (vhost != null) {
            if (header != null) {
                header = new httpHeader();
            }
            // set host-header
            header.add(httpHeader.HOST, vhost);
        }
        return header;
    }

    /**
     * Gets a page-header
     * 
     * @param uri
     * @return
     */
    public static httpHeader whead(final String uri) {
        return whead(uri, null);
    }

    /**
     * Gets a page-header
     * 
     * @param uri
     * @param header request header
     * @return
     */
    public static httpHeader whead(final String uri, final httpHeader header) {
        final de.anomic.http.HttpClient client = HttpFactory.newClient();
        if (header != null) {
            client.setHeader(header);
        }
        try {
            final HttpResponse response = client.HEAD(uri);
            return response.getResponseHeader();
        } catch (final IOException e) {
            serverLog.logWarning("HTTPC", "whead(" + uri + ") failed: " + e.getMessage());
        }
        return null;
    }
}