Android Open Source - FxExplorer Http Connection






From Project

Back to project page FxExplorer.

License

The source code is released under:

Apache License

If you think the Android project FxExplorer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.gescobar.httpserver;
/*from   www . jav a  2s .  c o  m*/
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import info.breezes.toolkit.log.Log;

/**
 * Handles an HTTP client connection. It parses the request creating an {@link net.gescobar.httpserver.Request} and a {@link net.gescobar.httpserver.Response}
 * implementations that are passed to the {@link net.gescobar.httpserver.Handler} implementation.
 *
 * @author German Escobar
 */
public class HttpConnection implements Runnable {

    /**
     * The socket with the underlying connection.
     */
    private Socket socket;

    /**
     * The implementation that will handle requests.
     */
    private Handler handler;

    /**
     * Constructor.
     *
     * @param socket
     * @param handler
     */
    public HttpConnection(Socket socket, Handler handler) {
        this.socket = socket;
        this.handler = handler;
    }

    @Override
    public void run() {
        Log.i(null, "handling HTTP request ... ");
        try {
            InputStream is = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            // build the request and response object
            RequestImpl request = new RequestImpl(reader);
            OutputStream os = socket.getOutputStream();
            Response response = new Response(os);
            handler.handle(request, response);
            if (!response.isFinished()) {
                response.finish();
            }
            Log.i(null, "http request finished");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * This is an internal implementation of the {@link net.gescobar.httpserver.Request} interface.
     *
     * @author German Escobar
     */
    private class RequestImpl implements Request {

        /**
         * The Host header.
         */
        private String host;

        /**
         * The HTTP method
         */
        private String method;

        /**
         * The request path
         */
        private String path;

        /**
         * The request headers
         */
        private Map<String, String> headers;

        /**
         * Constructor.
         *
         * @param reader from which we are reading the headers.
         * @throws java.io.IOException if an I/O error occurs in the underlying connection.
         */
        public RequestImpl(BufferedReader reader) throws IOException {

            String request = reader.readLine();
            Log.d(null,request);
            if(TextUtils.isEmpty(request)){
                throw new IOException("cannot read request header");
            }
            // get the method and the path
            method = request.split(" ")[0];
            path = request.split(" ")[1];

            // get the headers
            headers = retrieveHeaders(reader);

        }

        /**
         * Helper method. Retrieves the headers of the request.
         *
         * @param reader the reader from which we are retrieving the request information.
         * @return a Map<String,String> object with the headers of the request.
         * @throws java.io.IOException if an I/O error occurs in the underlying communication.
         */
        private Map<String, String> retrieveHeaders(BufferedReader reader) throws IOException {

            Map<String, String> headers = new HashMap<String, String>();

            // iterate through the headers
            String headerLine = reader.readLine();
            while (!headerLine.equals("")) {

                // headers come in the form "name: value"
                String name = headerLine.split(":")[0].trim();
                String value = headerLine.split(":")[1].trim();

                // add to the headers only if there is no corresponding field (e.g. "Host" header is mapped to the
                // *host* field of the request)
                if (!isKnownHeader(name, value)) {
                    headers.put(name, value);
                }

                // read next line
                headerLine = reader.readLine();
            }

            return headers;

        }

        /**
         * Checks if it is a known header and sets the corresponding field.
         *
         * @param name  the name of the header to check.
         * @param value the value of the header to check.
         * @return true if it is a known header, false otherwise
         */
        private boolean isKnownHeader(String name, String value) {

            boolean ret = false;

            if (name.equalsIgnoreCase("host")) {
                host = value;
                return true;
            }

            return ret;

        }

        @Override
        public String getMethod() {
            return method;
        }

        @Override
        public String getPath() {
            return path;
        }

        @Override
        public String getHost() {
            return host;
        }

        @Override
        public Map<String, String> getHeaders() {
            return headers;
        }

        @Override
        public String getHeader(String name) {
            return headers.get(name);
        }

    }

}




Java Source Code List

info.breezes.fx.downloader.ApplicationTest.java
info.breezes.fx.downloader.DlMainActivity.java
info.breezes.fx.editor.ApplicationTest.java
info.breezes.fx.editor.EditorMainActivity.java
info.breezes.fx.player.ApplicationTest.java
info.breezes.fx.player.MainActivity.java
info.breezes.fx.viewer.ApplicationTest.java
info.breezes.fx.viewer.BigImageView.java
info.breezes.fx.viewer.ImageUtility.java
info.breezes.fx.viewer.MainActivity.java
info.breezes.fxmanager.ApplicationTest.java
info.breezes.fxmanager.FxApplication.java
info.breezes.fxmanager.LocalFileSystemProvider.java
info.breezes.fxmanager.MainActivity.java
info.breezes.fxmanager.MediaFragment.java
info.breezes.fxmanager.MediaItemUtil.java
info.breezes.fxmanager.MediaProvider.java
info.breezes.fxmanager.MenuAdapter.java
info.breezes.fxmanager.MimeTypeMap.java
info.breezes.fxmanager.NetUtils.java
info.breezes.fxmanager.PackagesProvider.java
info.breezes.fxmanager.ScanResultActivity.java
info.breezes.fxmanager.ScannerActivity.java
info.breezes.fxmanager.SettingsActivity.java
info.breezes.fxmanager.ShellUtil.java
info.breezes.fxmanager.StorageTool.java
info.breezes.fxmanager.ThemeChooserActivity.java
info.breezes.fxmanager.android.app.QAlertDialog.java
info.breezes.fxmanager.countly.CountlyActivity.java
info.breezes.fxmanager.countly.CountlyEvent.java
info.breezes.fxmanager.countly.CountlyFragment.java
info.breezes.fxmanager.countly.CountlyUtils.java
info.breezes.fxmanager.dialog.ApkInfoDialog.java
info.breezes.fxmanager.dialog.FileInfoDialog.java
info.breezes.fxmanager.dialog.HashInfoDialog.java
info.breezes.fxmanager.model.DrawerMenu.java
info.breezes.fxmanager.model.MediaItem.java
info.breezes.fxmanager.qrcode.QrBitmapDecoder.java
info.breezes.fxmanager.service.FileService.java
net.gescobar.httpserver.Handler.java
net.gescobar.httpserver.HttpConnection.java
net.gescobar.httpserver.HttpServer.java
net.gescobar.httpserver.Request.java
net.gescobar.httpserver.Response.java