11 February 2014

POST requests in Java application

If you need more control over the POST requests, you might wish to implement your own POST handler within your Java server application.

Then you'll come across three ENCTYPEs to deal with:

  1. application/x-www-form-urlencoded:
    read the stream (message body), parse and URL decode the fields (key-value pairs);

  2. text/plain:
    read the stream and... be happy. No need to decode or parse, as the it arrives just as it was sent. ;

  3. multipart/form-data:
    here everything gets more complicated, since the stream is organised in packages, sent one by one. Each chunk might have multiple form fields, files or parts of the file. Handling the files is a bit tricky, because in one chunk the file could be started, then several chunk could convey parts of this large file, then it could be consumed in another chunk.
For files, there are two main strategies:
  • read all the chunks into one big buffer, and then parse it as one huge string, by splitting it into sections, parsing each header of the section and retrieving its value. The obvious downside of such an approach is that you consume a lot of resources, by which reason many POST implementations restrict the transmission to 2Mb, 5Mb, 10Mb, allocated for the entire transmission;

  • another strategy is to deal with each chunk apart and as it is available for reading. It allows you to use the minimum of resources along with no restrictions on the transmission. The core of this approach is marking the points when the file starts and when it ends, which might occur in different chunks/packages.
The example below implements the second approach: chunk by chunk, no limits, low resources. The application is minimalistic too and provides only one route (context):
  • for the GET method, which delivers an HTML with three ENCTYPEs (x-www-form, plain text and multipart) to experiment with:

X-WWW-Form:

First name:


Last name:


Text/Plain:

First name:


Last name:


Multipart:

First name:


Last name:





  • and one POST channel to consume the three POST enctype submissions. For that, we wrote the class doPost with three main methods, respectively, as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import java.io.*;
import java.util.*;
import com.sun.net.httpserver.*;

public class httpserver {

  public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new java.net.InetSocketAddress(8080), 0);
    server.createContext("/", new info());
    server.setExecutor(null);
    server.start();
    System.out.println("> Server running on PORT: 8080...");
  }

  static class info implements HttpHandler {
    public void handle(HttpExchange t) {
      
      if("POST".equals(t.getRequestMethod())) { new doPost(t); return; }

      String RES = "<html><head><style>input { margin-bottom: 10px; }</style></head><body><table border='1' cellpadding='12' bgcolor='#eee'><tr>";

      RES += "<td valign='top'><h3>X-Form:</h3><form action='/' method='post' enctype='application/x-www-form-urlencoded'>";
      RES += "First name:<br><input type='text' id='fname' name='fname' value='First Name'><br>";
      RES += "Last name:<br><input type='text' id='lname' name='lname' value='Last Name'><br>";
      RES += "<input type='submit' value='Submit'>";
      RES += "</form></td>";

      RES += "<td valign='top'><h3>Text/Plain:</h3><form action='/' method='post' enctype='text/plain'>";
      RES += "First name:<br><input type='text' id='fname' name='fname' value='First Name'><br>";
      RES += "Last name:<br><input type='text' id='lname' name='lname' value='Last Name'><br>";
      RES += "<input type='submit' value='Submit'>";
      RES += "</form></td>";

      RES += "<td valign='top'><h3>Multipart:</h3><form action='/' method='post' enctype='multipart/form-data'>";
      RES += "First name:<br><input type='text' id='fname' name='fname' value='First Name'><br>";
      RES += "Last name:<br><input type='text' id='lname' name='lname' value='Last Name'><br>";
      RES += "<input type='file' id='myfile' name='myfile'><br>";
      RES += "<input type='submit' value='Submit'>";
      RES += "</form></td>";
      RES += "</tr></table></body></html>";

      try {
        t.getResponseHeaders().set("Content-Type", "text/html");
        t.sendResponseHeaders(200, 0);
        OutputStream os = t.getResponseBody();
        os.write(RES.getBytes()); os.close();
      } catch(IOException ex) { ex.printStackTrace(); }
    }
  }

/* --- Class to handle POST requests --- */
static class doPost {

  private String bndry, contType;
  private int contLength;
  private Map<String, String> data;

  public doPost(HttpExchange t) { // constructor
    getParams(t);
    if(contType.equals("application/x-www-form-urlencoded")) { xForm(t); return; }
    else if(contType.equals("text/plain")) { textPlain(t); return; }
    else if(contType.equals("multipart/form-data")) { multiPart(t); return; }
    else {
      pr("\nContent-Type: " + contType + "\nContent-Length: " + contLength + "\nBoundary: [" + bndry + "]\n");
      writeBack(t, "Unknown request ENCTYPE!".getBytes());
      return;
    }
  }

  void xForm(HttpExchange t) { // handle application/x-www-form-urlencoded
    writeBack(t, mapQueries(getBody(t)) ? getMapItems().getBytes() : "Unresolved queries.".getBytes());
  }

  void textPlain(HttpExchange t) { // handle text/plain
    writeBack(t, getBody(t).getBytes());
  }

  void multiPart(HttpExchange t) { // handle multipart/form-data
    if(bndry == null) return;

    String FL = "";
    boolean fFlag = false;
    byte[] chunk;
    int bLen = bndry.length();
    Map<String, String> FLDS = new HashMap<String, String>();
    
    while(true) {
      
      try {
        chunk = t.getRequestBody().readAllBytes();
        if(chunk == null ) return;
      } catch(IOException ioe) { ioe.printStackTrace(); return; }

      pr("\n ---- NEW CHUNK ----\n");
      int ix = 0;
      boolean first = true;
      while(true) {

        ix = find(chunk, bndry, ix);
        if(ix < 0) {
          if(fFlag) {
            pr("Continue reading file.");
            FL += chunk;
            break;
          }
          else { pr("Uncaught error"); return; }
        }

        if(first) {
          first = false;
          if(fFlag && ix > 0) {
            pr("Finishing reading.");
            FL += cut(chunk, 0, find(chunk, bndry, 0)-2);
            fFlag = false;
            continue;
          }
        }

        if(chunk[ix + bLen] == '-' && chunk[ix + bLen + 1] == '-'){
            data = FLDS;
            writeBack(t, (getMapItems() + (FL.isEmpty()?"":("\n" + FL + ']'))).getBytes());
            pr("FINISH");
            return;
        }

        fFlag = false;

        int fi = find(chunk, "\r\n\r\n", ix + bLen + 2);
        parseHeader(cut(chunk, ix+bLen+2, fi));
        //pr(getMapItems());

        String fileName = data.get("filename");
        if(fileName != null) {
          pr("File: " + fileName + ", Type: " + data.get("Content-Type"));
          if(fileName.isEmpty()) {
            pr("No file sent.");
            ix = fi + 4;
            continue;
          }

          ix = find(chunk, bndry, fi + 4);
          if(ix < 0) ix = chunk.length;
          FLDS.put(data.get("name"), fileName);
          FL += fileName + '[' + cut(chunk, fi+4, ix-2);
          fFlag = true;
        }
        else {
          ix = find(chunk, bndry, fi + 4);
          if(ix < 0) ix = chunk.length;
          FLDS.put(data.get("name"), cut(chunk, fi+4, ix-2).toString());
          pr("VALUE = [" + cut(chunk, fi+4, ix-2).toString() + ']');
          ix = fi + 4;
        }
      }
    }    
  }

  void writeBack(HttpExchange t, byte[] str) {
    try {
      t.getResponseHeaders().set("Content-Type", "text/plain");
      t.sendResponseHeaders(200, str.length); // or 0 instead of str.length
      OutputStream os = t.getResponseBody();
      os.write(str); os.flush(); os.close();        
    } catch(IOException ioe) { pr("ACHTUNG!"); ioe.printStackTrace(); }
  }

  static String cut(byte[] src, int start, int end) {
    byte[] res = new byte[end-start];
    System.arraycopy(src, start, res, 0, end-start);
    return new String(res);
  }

  static int find(byte[] in, String whatin, int startAt) {
    byte[] what = whatin.getBytes();
    int inLen = in.length, whatLen = what.length, i, j;
    for(i = startAt; i < inLen; i++)
      if(in[i] == what[0]) {
        j=1; while(j < whatLen && in[i+j] == what[j]) j++;
        if(j == whatLen) return i;
      }
    return -1;
  }

  String getMapItems() {
    String qs = "";
    for(Map.Entry<String,String> el : data.entrySet())
      qs += el.getKey()+ '=' + el.getValue() + '\n';
    return qs;
  }

  String getBody(HttpExchange t) {
    try {
      return new String(t.getRequestBody().readAllBytes());
    } catch(IOException ioe) { ioe.printStackTrace(); return null; }
  }

  boolean mapQueries(String q) {
    if(q == null) return false;
    String[] arr = q.split("&");
    data = new HashMap<String, String>();
    try {
      for(int i=0; i<arr.length; i++) {
        int ix = arr[i].indexOf('='); if(ix<0) return false;
        data.put(arr[i].substring(0, ix), java.net.URLDecoder.decode(arr[i].substring(ix+1), "UTF-8"));
      }
    } catch(UnsupportedEncodingException ex) { ex.printStackTrace(); return false; }
    return true;
  }

  void getParams(HttpExchange t) {
    contType = t.getRequestHeaders().get("Content-Type").get(0);
    contLength = Integer.parseInt(t.getRequestHeaders().get("Content-Length").get(0));
    int ix = contType.indexOf("boundary=");
    if(ix < 0) { bndry = null; }
    else { bndry = "--" + contType.substring(ix + 9); contType = contType.substring(0, ix-2); }
    return;
  }

  void parseHeader(String hd) {
    data = new HashMap<String, String>();
    String[] arr = hd.split("; |\r\n");
    for(String el: arr) {
      if(el.isEmpty()) continue;
      String[] pairs = el.split(": |=");
      if(pairs.length < 2) data.put("value", pairs[0]);
      else data.put(pairs[0], pairs[1].indexOf('"') < 0?pairs[1]:pairs[1].substring(1,pairs[1].length()-1));
    }
    return;
  }

  void pr(String s) { System.out.println(s); }
}

}

No comments:

Post a Comment