1a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath/*
2a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Copyright (C) 2006 The Android Open Source Project
3a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath *
4a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Licensed under the Apache License, Version 2.0 (the "License");
5a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * you may not use this file except in compliance with the License.
6a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * You may obtain a copy of the License at
7a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath *
8a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath *      http://www.apache.org/licenses/LICENSE-2.0
9a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath *
10a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Unless required by applicable law or agreed to in writing, software
11a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * distributed under the License is distributed on an "AS IS" BASIS,
12a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * See the License for the specific language governing permissions and
14a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * limitations under the License.
15a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */
16a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
17a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathpackage android.net.http;
18a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
19a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
20a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath/**
21a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Callbacks in this interface are made as an HTTP request is
22a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * processed. The normal order of callbacks is status(), headers(),
23a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * then multiple data() then endData().  handleSslErrorRequest(), if
24a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * there is an SSL certificate error. error() can occur anywhere
25a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * in the transaction.
26a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */
27a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
28a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathpublic interface EventHandler {
29a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
30a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
31a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * Error codes used in the error() callback.  Positive error codes
32a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * are reserved for codes sent by http servers.  Negative error
33a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * codes are connection/parsing failures, etc.
34a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
35a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
36a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Success */
37a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int OK = 0;
38a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Generic error */
39a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR = -1;
40a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Server or proxy hostname lookup failed */
41a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_LOOKUP = -2;
42a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Unsupported authentication scheme (ie, not basic or digest) */
43a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
44a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** User authentication failed on server */
45a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_AUTH = -4;
46a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** User authentication failed on proxy */
47a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_PROXYAUTH = -5;
48a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Could not connect to server */
49a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_CONNECT = -6;
50a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Failed to write to or read from server */
51a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_IO = -7;
52a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Connection timed out */
53a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_TIMEOUT = -8;
54a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Too many redirects */
55a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_REDIRECT_LOOP = -9;
56a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Unsupported URI scheme (ie, not http, https, etc) */
57a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_UNSUPPORTED_SCHEME = -10;
58a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Failed to perform SSL handshake */
59a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
60a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Bad URL */
61a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int ERROR_BAD_URL = -12;
62a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Generic file error for file:/// loads */
63a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int FILE_ERROR = -13;
64a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** File not found error for file:/// loads */
65a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int FILE_NOT_FOUND_ERROR = -14;
66a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /** Too many requests queued */
67a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public static final int TOO_MANY_REQUESTS_ERROR = -15;
68a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
69a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
70a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * Called after status line has been sucessfully processed.
71a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param major_version HTTP version advertised by server.  major
72a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * is the part before the "."
73a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param minor_version HTTP version advertised by server.  minor
74a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * is the part after the "."
75a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param code HTTP Status code.  See RFC 2616.
76a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param reason_phrase Textual explanation sent by server
77a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
78a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public void status(int major_version,
79a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath                       int minor_version,
80a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath                       int code,
81a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath                       String reason_phrase);
82a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
83a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
84a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * Called after all headers are successfully processed.
85a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
86a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public void headers(Headers headers);
87a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
88a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
89a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * An array containing all or part of the http body as read from
90a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * the server.
91a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param data A byte array containing the content
92a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param len The length of valid content in data
93a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     *
94a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * Note: chunked and compressed encodings are handled within
95a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * android.net.http.  Decoded data is passed through this
96a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * interface.
97a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
98a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public void data(byte[] data, int len);
99a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
100a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
101a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * Called when the document is completely read.  No more data()
102a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * callbacks will be made after this call
103a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
104a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public void endData();
105a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
106a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
107a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * SSL certificate callback called before resource request is
108a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * made, which will be null for insecure connection.
109a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
110a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public void certificate(SslCertificate certificate);
111a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
112a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
113a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * There was trouble.
114a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param id One of the error codes defined below
115a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @param description of error
116a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
117a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public void error(int id, String description);
118a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
119a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    /**
120a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * SSL certificate error callback. Handles SSL error(s) on the way
121a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * up to the user. The callback has to make sure that restartConnection() is called,
122a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * otherwise the connection will be suspended indefinitely.
123a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     * @return True if the callback can handle the error, which means it will
124a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     *              call restartConnection() to unblock the thread later,
125a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     *              otherwise return false.
126a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath     */
127a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath    public boolean handleSslErrorRequest(SslError error);
128a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath
129a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath}
130