Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame^] | 1 | /*
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one
|
| 3 | * or more contributor license agreements. See the NOTICE file
|
| 4 | * distributed with this work for additional information
|
| 5 | * regarding copyright ownership. The ASF licenses this file
|
| 6 | * to you under the Apache License, Version 2.0 (the
|
| 7 | * "License"); you may not use this file except in compliance
|
| 8 | * with the License. You may obtain a copy of the License at
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 9 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame^] | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 11 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame^] | 12 | * Unless required by applicable law or agreed to in writing,
|
| 13 | * software distributed under the License is distributed on an
|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 15 | * KIND, either express or implied. See the License for the
|
| 16 | * specific language governing permissions and limitations
|
| 17 | * under the License.
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 18 | */
|
| 19 |
|
| 20 | package org.apache.felix.upnp.basedriver.importer.util;
|
| 21 |
|
| 22 | import java.io.*;
|
| 23 | import java.net.*;
|
| 24 |
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 25 | /*
|
| 26 | * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
|
| 27 | */
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 28 | public class HTTPRequestForIcon {
|
| 29 | private URL url;
|
| 30 |
|
| 31 | public HTTPRequestForIcon(URL url) {
|
| 32 | this.url = url;
|
| 33 | }
|
| 34 | public InputStream getInputStream() throws IOException {
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 35 | //TODO we should speak about that
|
| 36 | InetAddress inet = InetAddress.getByName(url.getHost());
|
| 37 | int port = url.getPort();
|
| 38 | Socket socket = null;
|
| 39 | socket = new Socket(inet, port);
|
| 40 | OutputStream out = null;
|
| 41 | out = socket.getOutputStream();
|
| 42 | String CRLF = "\r\n";
|
| 43 | url.getFile();
|
| 44 | String request = "GET " + url.getPath() + " " + "HTTP/1.1" + CRLF
|
| 45 | + "Host: " + url.getHost() + CRLF + "Connection: " + "close"
|
| 46 | + CRLF + CRLF;
|
| 47 | //System.out.println(request);
|
| 48 | byte[] get = request.getBytes();
|
| 49 | out.write(get, 0, get.length);
|
| 50 | InputStream in = socket.getInputStream();
|
| 51 | boolean exit = true;
|
| 52 | while (exit) {
|
| 53 | byte[] b = new byte[1];
|
| 54 | in.read(b, 0, b.length);
|
| 55 |
|
| 56 | if (b[0] == '\r') {
|
| 57 | in.read(b, 0, b.length);
|
| 58 | while (b[0] == '\r') {
|
| 59 | in.read(b, 0, b.length);
|
| 60 | }
|
| 61 | if (b[0] != '\n') {
|
| 62 | continue;
|
| 63 | }
|
| 64 | in.read(b, 0, b.length);
|
| 65 | if (b[0] != '\r') {
|
| 66 | continue;
|
| 67 | }
|
| 68 | in.read(b, 0, b.length);
|
| 69 | if (b[0] != '\n') {
|
| 70 | continue;
|
| 71 | }
|
| 72 | exit = false;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | return in;
|
| 77 |
|
| 78 | /*
|
Francesco Furfari | 0b466c7 | 2006-07-17 18:11:23 +0000 | [diff] [blame] | 79 | * HTTPResponse response=new HTTPResponse(in);
|
| 80 | * InputStream iconInStream=response.getContentInputStream();
|
| 81 | * return iconInStream;
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 82 | *
|
| 83 | */
|
| 84 | /*
|
| 85 | *
|
| 86 | * byte[] buff = new byte[maxLength]; int initial = 0; while (initial <
|
| 87 | * maxLength - 1) { int read = 0; read = in.read(buff, initial, 1024);
|
| 88 | * if (read == -1) break; initial += read; } System.out.println(new
|
| 89 | * String(buff));
|
| 90 | */
|
| 91 |
|
| 92 | }
|
| 93 |
|
| 94 | } |