Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.browser;
     18 
     19 import org.apache.http.Header;
     20 import org.apache.http.HttpHost;
     21 import org.apache.http.HttpResponse;
     22 import org.apache.http.client.methods.HttpHead;
     23 import org.apache.http.conn.params.ConnRouteParams;
     24 
     25 import android.app.DownloadManager;
     26 import android.content.Context;
     27 import android.net.Proxy;
     28 import android.net.http.AndroidHttpClient;
     29 import android.os.Environment;
     30 import android.webkit.MimeTypeMap;
     31 import android.webkit.URLUtil;
     32 
     33 import java.io.IOException;
     34 
     35 /**
     36  * This class is used to pull down the http headers of a given URL so that
     37  * we can analyse the mimetype and make any correction needed before we give
     38  * the URL to the download manager.
     39  * This operation is needed when the user long-clicks on a link or image and
     40  * we don't know the mimetype. If the user just clicks on the link, we will
     41  * do the same steps of correcting the mimetype down in
     42  * android.os.webkit.LoadListener rather than handling it here.
     43  *
     44  */
     45 class FetchUrlMimeType extends Thread {
     46 
     47     private Context mContext;
     48     private DownloadManager.Request mRequest;
     49     private String mUri;
     50     private String mCookies;
     51     private String mUserAgent;
     52 
     53     public FetchUrlMimeType(Context context, DownloadManager.Request request,
     54             String uri, String cookies, String userAgent) {
     55         mContext = context.getApplicationContext();
     56         mRequest = request;
     57         mUri = uri;
     58         mCookies = cookies;
     59         mUserAgent = userAgent;
     60     }
     61 
     62     @Override
     63     public void run() {
     64         // User agent is likely to be null, though the AndroidHttpClient
     65         // seems ok with that.
     66         AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
     67         HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, mUri);
     68         if (httpHost != null) {
     69             ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
     70         }
     71         HttpHead request = new HttpHead(mUri);
     72 
     73         if (mCookies != null && mCookies.length() > 0) {
     74             request.addHeader("Cookie", mCookies);
     75         }
     76 
     77         HttpResponse response;
     78         String mimeType = null;
     79         String contentDisposition = null;
     80         try {
     81             response = client.execute(request);
     82             // We could get a redirect here, but if we do lets let
     83             // the download manager take care of it, and thus trust that
     84             // the server sends the right mimetype
     85             if (response.getStatusLine().getStatusCode() == 200) {
     86                 Header header = response.getFirstHeader("Content-Type");
     87                 if (header != null) {
     88                     mimeType = header.getValue();
     89                     final int semicolonIndex = mimeType.indexOf(';');
     90                     if (semicolonIndex != -1) {
     91                         mimeType = mimeType.substring(0, semicolonIndex);
     92                     }
     93                 }
     94                 Header contentDispositionHeader = response.getFirstHeader("Content-Disposition");
     95                 if (contentDispositionHeader != null) {
     96                     contentDisposition = contentDispositionHeader.getValue();
     97                 }
     98             }
     99         } catch (IllegalArgumentException ex) {
    100             request.abort();
    101         } catch (IOException ex) {
    102             request.abort();
    103         } finally {
    104             client.close();
    105         }
    106 
    107        if (mimeType != null) {
    108            if (mimeType.equalsIgnoreCase("text/plain") ||
    109                    mimeType.equalsIgnoreCase("application/octet-stream")) {
    110                String newMimeType =
    111                        MimeTypeMap.getSingleton().getMimeTypeFromExtension(
    112                            MimeTypeMap.getFileExtensionFromUrl(mUri));
    113                if (newMimeType != null) {
    114                    mRequest.setMimeType(newMimeType);
    115                }
    116            }
    117            String filename = URLUtil.guessFileName(mUri, contentDisposition,
    118                 mimeType);
    119            mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
    120        }
    121 
    122        // Start the download
    123        DownloadManager manager = (DownloadManager) mContext.getSystemService(
    124                Context.DOWNLOAD_SERVICE);
    125        manager.enqueue(mRequest);
    126     }
    127 
    128 }
    129