src/java.base/share/classes/sun/net/www/MessageHeader.java
changeset 57713 0211b062843d
parent 47216 71c04702a3d5
child 57968 8595871a5446
equal deleted inserted replaced
57712:145300cc8ea6 57713:0211b062843d
   286         }
   286         }
   287 
   287 
   288         return Collections.unmodifiableMap(m);
   288         return Collections.unmodifiableMap(m);
   289     }
   289     }
   290 
   290 
       
   291     /** Check if a line of message header looks like a request line.
       
   292      * This method does not perform a full validation but simply
       
   293      * returns false if the line does not end with 'HTTP/[1-9].[0-9]'
       
   294      * @param line the line to check.
       
   295      * @return true if the line might be a request line.
       
   296      */
       
   297     private boolean isRequestline(String line) {
       
   298         String k = line.trim();
       
   299         int i = k.lastIndexOf(' ');
       
   300         if (i <= 0) return false;
       
   301         int len = k.length();
       
   302         if (len - i < 9) return false;
       
   303 
       
   304         char c1 = k.charAt(len-3);
       
   305         char c2 = k.charAt(len-2);
       
   306         char c3 = k.charAt(len-1);
       
   307         if (c1 < '1' || c1 > '9') return false;
       
   308         if (c2 != '.') return false;
       
   309         if (c3 < '0' || c3 > '9') return false;
       
   310 
       
   311         return (k.substring(i+1, len-3).equalsIgnoreCase("HTTP/"));
       
   312     }
       
   313 
       
   314 
   291     /** Prints the key-value pairs represented by this
   315     /** Prints the key-value pairs represented by this
   292         header.  Also prints the RFC required blank line
   316         header. Also prints the RFC required blank line
   293         at the end. Omits pairs with a null key. */
   317         at the end. Omits pairs with a null key. Omits
       
   318         colon if key-value pair is the requestline. */
   294     public synchronized void print(PrintStream p) {
   319     public synchronized void print(PrintStream p) {
   295         for (int i = 0; i < nkeys; i++)
   320         for (int i = 0; i < nkeys; i++)
   296             if (keys[i] != null) {
   321             if (keys[i] != null) {
   297                 p.print(keys[i] +
   322                 StringBuilder sb = new StringBuilder(keys[i]);
   298                     (values[i] != null ? ": "+values[i]: "") + "\r\n");
   323                 if (values[i] != null) {
       
   324                     sb.append(": " + values[i]);
       
   325                 } else if (i != 0 || !isRequestline(keys[i])) {
       
   326                     sb.append(":");
       
   327                 }
       
   328                 p.print(sb.append("\r\n"));
   299             }
   329             }
   300         p.print("\r\n");
   330         p.print("\r\n");
   301         p.flush();
   331         p.flush();
   302     }
   332     }
   303 
   333