4747import org .apache .commons .fileupload .MultipartStream ;
4848import org .joda .time .format .ISODateTimeFormat ;
4949
50- import java .io .BufferedReader ;
51- import java .io .ByteArrayInputStream ;
5250import java .io .ByteArrayOutputStream ;
5351import java .io .IOException ;
5452import java .io .InputStream ;
55- import java .io .InputStreamReader ;
5653import java .io .OutputStream ;
5754import java .math .BigInteger ;
5855import java .net .InetSocketAddress ;
6966import java .util .NavigableMap ;
7067import java .util .NavigableSet ;
7168import java .util .Random ;
69+ import java .util .Scanner ;
7270import java .util .Set ;
7371import java .util .SortedMap ;
7472import java .util .TreeMap ;
@@ -119,15 +117,9 @@ public class LocalDnsHelper {
119117 private static final ScheduledExecutorService EXECUTORS =
120118 Executors .newScheduledThreadPool (2 , Executors .defaultThreadFactory ());
121119 private static final String PROJECT_ID = "dummyprojectid" ;
122- private static final String responseBoundary = "____THIS_IS_HELPERS_BOUNDARY____" ;
123- private static final String responseSeparator = new StringBuilder ("--" )
124- .append (responseBoundary )
125- .append ("\r \n " )
126- .toString ();
127- private static final String responseEnd = new StringBuilder ("--" )
128- .append (responseBoundary )
129- .append ("--\r \n \r \n " )
130- .toString ();
120+ private static final String RESPONSE_BOUNDARY = "____THIS_IS_HELPERS_BOUNDARY____" ;
121+ private static final String RESPONSE_SEPARATOR = "--" + RESPONSE_BOUNDARY + "\r \n " ;
122+ private static final String RESPONSE_END = "--" + RESPONSE_BOUNDARY + "--\r \n \r \n " ;
131123
132124 static {
133125 try {
@@ -333,7 +325,6 @@ private Response pickHandler(HttpExchange exchange, CallRegex regex) {
333325 try {
334326 return handleBatch (exchange );
335327 } catch (IOException ex ) {
336- ex .printStackTrace ();
337328 return Error .BAD_REQUEST .response (ex .getMessage ());
338329 }
339330 default :
@@ -377,19 +368,22 @@ private Response handleBatch(final HttpExchange exchange) throws IOException {
377368 OutputStream socketOutput = socket .getOutputStream ();
378369 ByteArrayOutputStream section = new ByteArrayOutputStream ();
379370 multipartStream .readBodyData (section );
380- BufferedReader reader = new BufferedReader (
381- new InputStreamReader (new ByteArrayInputStream (section .toByteArray ())));
382371 String line ;
383372 String contentId = null ;
384- while (!(line = reader .readLine ()).isEmpty ()) {
385- if (line .toLowerCase ().startsWith ("content-id" )) {
373+ Scanner scanner = new Scanner (new String (section .toByteArray ()));
374+ while (scanner .hasNextLine ()) {
375+ line = scanner .nextLine ();
376+ if (line .isEmpty ()) {
377+ break ;
378+ } else if (line .toLowerCase ().startsWith ("content-id" )) {
386379 contentId = line .split (":" )[1 ].trim ();
387380 }
388381 }
389- String requestLine = reader . readLine ();
382+ String requestLine = scanner . nextLine ();
390383 socketOutput .write ((requestLine + " \r \n " ).getBytes ());
391384 socketOutput .write ("Connection: close \r \n " .getBytes ());
392- while ((line = reader .readLine ()) != null ) {
385+ while (scanner .hasNextLine ()) {
386+ line = scanner .nextLine ();
393387 socketOutput .write (line .getBytes ());
394388 if (!line .isEmpty ()) {
395389 socketOutput .write (" \r \n " .getBytes ());
@@ -400,7 +394,7 @@ private Response handleBatch(final HttpExchange exchange) throws IOException {
400394 socketOutput .flush ();
401395 InputStream in = socket .getInputStream ();
402396 int length ;
403- out .write (responseSeparator .getBytes ());
397+ out .write (RESPONSE_SEPARATOR .getBytes ());
404398 out .write ("Content-Type: application/http \r \n " .getBytes ());
405399 out .write (("Content-ID: " + contentId + " \r \n \r \n " ).getBytes ());
406400 try {
@@ -411,8 +405,10 @@ private Response handleBatch(final HttpExchange exchange) throws IOException {
411405 // this handles connection reset error
412406 }
413407 }
414- out .write (responseEnd .getBytes ());
408+ out .write (RESPONSE_END .getBytes ());
415409 writeBatchResponse (exchange , out );
410+ } else {
411+ return Error .BAD_REQUEST .response ("Content-type header was not provided for batch." );
416412 }
417413 return null ;
418414 }
@@ -520,14 +516,14 @@ private static void writeResponse(HttpExchange exchange, Response response) {
520516 }
521517 }
522518
523- private static void writeBatchResponse (HttpExchange exchange , ByteArrayOutputStream out ) {
519+ private static void writeBatchResponse (HttpExchange exchange , ByteArrayOutputStream output ) {
524520 exchange .getResponseHeaders ().set (
525- "Content-type" , "multipart/mixed; boundary=" + responseBoundary );
521+ "Content-type" , "multipart/mixed; boundary=" + RESPONSE_BOUNDARY );
526522 try {
527523 exchange .getResponseHeaders ().add ("Connection" , "close" );
528- exchange .sendResponseHeaders (200 , out .toByteArray ().length );
524+ exchange .sendResponseHeaders (200 , output .toByteArray ().length );
529525 OutputStream responseBody = exchange .getResponseBody ();
530- out .writeTo (responseBody );
526+ output .writeTo (responseBody );
531527 responseBody .close ();
532528 } catch (IOException e ) {
533529 log .log (Level .WARNING , "IOException encountered when sending response." , e );
0 commit comments