jdk/test/sun/net/www/httptest/HttpTransaction.java
author duke
Wed, 05 Jul 2017 16:48:18 +0200
changeset 2092 b86b8d6d48bd
parent 2 90ce3da70b43
child 3946 7ee79633ec36
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class encapsulates a HTTP request received and a response to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * generated in one transaction. It provides methods for examaining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * request from the client, and for building and sending a reply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class HttpTransaction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    String command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    URI requesturi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    HttpServer.Server server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    MessageHeader reqheaders, reqtrailers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    String reqbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    byte[] rspbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    MessageHeader rspheaders, rsptrailers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    SelectionKey  key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int rspbodylen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    boolean rspchunked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    HttpTransaction (HttpServer.Server server, String command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                        URI requesturi, MessageHeader headers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                        String body, MessageHeader trailers, SelectionKey  key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.command = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.requesturi = requesturi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.reqheaders = headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.reqbody = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.reqtrailers = trailers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Get the value of a request header whose name is specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * String argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param key the name of the request header
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @return the value of the header or null if it does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public String getRequestHeader (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return reqheaders.findValue (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Get the value of a response header whose name is specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * String argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param key the name of the response header
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the value of the header or null if it does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getResponseHeader (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return rspheaders.findValue (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Get the request URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @return the request URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public URI getRequestURI () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return requesturi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public String toString () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        buf.append ("Request from: ").append (key.channel().toString()).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        buf.append ("Command: ").append (command).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        buf.append ("Request URI: ").append (requesturi).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        buf.append ("Headers: ").append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        buf.append (reqheaders.toString()).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        buf.append ("Body: ").append (reqbody).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        buf.append ("---------Response-------\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        buf.append ("Headers: ").append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (rspheaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            buf.append (rspheaders.toString()).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        buf.append ("Body: ").append (new String(rspbody)).append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return new String (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Get the value of a request trailer whose name is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * the String argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param key the name of the request trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return the value of the trailer or null if it does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public String getRequestTrailer (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return reqtrailers.findValue (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Add a response header to the response. Multiple calls with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * key value result in multiple header lines with the same key identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param key the name of the request header to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param val the value of the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void addResponseHeader (String key, String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (rspheaders == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            rspheaders = new MessageHeader ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        rspheaders.add (key, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Set a response header. Searches for first header with named key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * and replaces its value with val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param key the name of the request header to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param val the value of the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void setResponseHeader (String key, String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (rspheaders == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            rspheaders = new MessageHeader ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        rspheaders.set (key, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Add a response trailer to the response. Multiple calls with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * key value result in multiple trailer lines with the same key identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param key the name of the request trailer to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param val the value of the trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void addResponseTrailer (String key, String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (rsptrailers == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            rsptrailers = new MessageHeader ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        rsptrailers.add (key, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Get the request method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the request method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public String getRequestMethod (){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Perform an orderly close of the TCP connection associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * request. This method guarantees that any response already sent will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * not be reset (by this end). The implementation does a shutdownOutput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * of the TCP connection and for a period of time consumes and discards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * data received on the reading side of the connection. This happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * in the background. After the period has expired the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * connection is completely closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void orderlyClose () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            server.orderlyCloseChannel (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Do an immediate abortive close of the TCP connection associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * with this request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void abortiveClose () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            server.abortiveCloseChannel(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Get the SocketChannel associated with this request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return the socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public SocketChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return (SocketChannel) key.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Get the request entity body associated with this request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * as a single String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return the entity body in one String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public String getRequestEntityBody (){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return reqbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Set the entity response body with the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * The content length is set to the length of the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param body the string to send in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void setResponseEntityBody (String body){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        rspbody = body.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        rspbodylen = body.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        rspchunked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        addResponseHeader ("Content-length", Integer.toString (rspbodylen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Set the entity response body with the given byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The content length is set to the gven length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param body the string to send in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public void setResponseEntityBody (byte[] body, int len){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        rspbody = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        rspbodylen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        rspchunked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        addResponseHeader ("Content-length", Integer.toString (rspbodylen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Set the entity response body by reading the given inputstream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param is the inputstream from which to read the body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void setResponseEntityBody (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte[] buf = new byte [2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        byte[] total = new byte [2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int total_len = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int c, len=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        while ((c=is.read (buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (len+c > total_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                byte[] total1 = new byte [total_len * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                System.arraycopy (total, 0, total1, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                total = total1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                total_len = total_len * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            System.arraycopy (buf, 0, total, len, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            len += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        setResponseEntityBody (total, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /* chunked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Set the entity response body with the given array of strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * The content encoding is set to "chunked" and each array element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * is sent as one chunk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param body the array of string chunks to send in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public void setResponseEntityBody (String[] body) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        StringBuffer buf = new StringBuffer ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        for (int i=0; i<body.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            String chunklen = Integer.toHexString (body[i].length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            len += body[i].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            buf.append (chunklen).append ("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            buf.append (body[i]).append ("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        buf.append ("0\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        rspbody = new String (buf).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        rspbodylen = rspbody.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        rspchunked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        addResponseHeader ("Transfer-encoding", "chunked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Send the response with the current set of response parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * but using the response code and string tag line as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param rCode the response code to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param rTag the response string to send with the response code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void sendResponse (int rCode, String rTag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        OutputStream os = new HttpServer.NioOutputStream(channel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        PrintStream ps = new PrintStream (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (rspheaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            rspheaders.print (ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            ps.print ("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        ps.flush ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (rspbody != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            os.write (rspbody, 0, rspbodylen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (rsptrailers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            rsptrailers.print (ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } else if (rspchunked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            ps.print ("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        ps.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /* sends one byte less than intended */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void sendPartialResponse (int rCode, String rTag)throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        OutputStream os = new HttpServer.NioOutputStream(channel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        PrintStream ps = new PrintStream (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        ps.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (rspbody != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            os.write (rspbody, 0, rspbodylen-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (rsptrailers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            rsptrailers.print (ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        ps.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}