Skip to content

Commit 214b471

Browse files
committed
fixes for request headers in HttpConnection and POST caching
- note that we generally do not want to send headers via AJAX CORS, because servers have to be set up specially to accept headers. Even Cache-Control requires permission. - adds hashcode for ByteArrayOutputStream
1 parent 4acdbd2 commit 214b471

File tree

5 files changed

+206
-118
lines changed

5 files changed

+206
-118
lines changed

sources/net.sf.j2s.java.core/src/java/io/BufferedOutputStream.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,13 @@ public synchronized void write(int oneByte) throws IOException {
176176
}
177177
buf[count++] = (byte) oneByte;
178178
}
179+
180+
@Override
181+
public int hashCode() {
182+
try {
183+
flush();
184+
} catch (IOException e) {
185+
}
186+
return out.hashCode();
187+
}
179188
}

sources/net.sf.j2s.java.core/src/java/io/ByteArrayOutputStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,10 @@ public synchronized String toString(int hibyte) {
282282
@Override
283283
public void close() throws IOException {
284284
}
285+
286+
@Override
287+
public int hashCode() {
288+
return buf.hashCode();
289+
}
285290

286291
}

sources/net.sf.j2s.java.core/src/java/net/URLConnection.java

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.List;
3737
import java.util.Map;
3838

39-
import javajs.util.Lst;
39+
import sun.net.www.MessageHeader;
4040

4141
/**
4242
* The abstract class <code>URLConnection</code> is the superclass of all
@@ -368,7 +368,7 @@ public OutputStream getOutputStream() throws IOException {
368368
throw new UnknownServiceException("protocol doesn't support output");
369369
}
370370

371-
private Lst<String[]> requests;
371+
private MessageHeader requests;
372372

373373
/**
374374
* Sets the general request property. If a property with the key already
@@ -395,14 +395,9 @@ public void setRequestProperty(String key, String value) {
395395
throw new IllegalStateException("Already connected");
396396
if (key == null)
397397
throw new NullPointerException("key is null");
398-
if (requests == null)
399-
requests = new Lst<String[]>();
400-
for (int i = requests.size(); --i >= 0;)
401-
if (requests.get(i)[0].equals(key)) {
402-
requests.get(i)[1] = value;
403-
return;
404-
}
405-
requests.addLast(new String[] { key, value });
398+
if (requests == null)
399+
requests = new MessageHeader();
400+
requests.set(key, value);
406401
}
407402

408403

@@ -548,73 +543,67 @@ public void setDefaultUseCaches(boolean defaultusecaches) {
548543
}
549544

550545

551-
/**
552-
* Adds a general request property specified by a
553-
* key-value pair. This method will not overwrite
554-
* existing values associated with the same key.
555-
*
556-
* @param key the keyword by which the request is known
557-
* (e.g., "<code>accept</code>").
558-
* @param value the value associated with it.
559-
* @throws IllegalStateException if already connected
560-
* @throws NullPointerException if key is null
561-
* @see #getRequestProperties()
562-
* @since 1.4
563-
*/
564-
public void addRequestProperty(String key, String value) {
565-
if (connected)
566-
throw new IllegalStateException("Already connected");
567-
if (key == null)
568-
throw new NullPointerException ("key is null");
569-
570-
// if (requests == null)
571-
// requests = new MessageHeader();
572-
//
573-
// requests.add(key, value);
574-
}
546+
/**
547+
* Adds a general request property specified by a key-value pair. This method
548+
* will not overwrite existing values associated with the same key.
549+
*
550+
* @param key the keyword by which the request is known (e.g.,
551+
* "<code>accept</code>").
552+
* @param value the value associated with it.
553+
* @throws IllegalStateException if already connected
554+
* @throws NullPointerException if key is null
555+
* @see #getRequestProperties()
556+
* @since 1.4
557+
*/
558+
public void addRequestProperty(String key, String value) {
559+
if (connected)
560+
throw new IllegalStateException("Already connected");
561+
if (key == null)
562+
throw new NullPointerException("key is null");
575563

564+
if (requests == null)
565+
requests = new MessageHeader();
576566

577-
/**
578-
* Returns the value of the named general request property for this
579-
* connection.
580-
*
581-
* @param key the keyword by which the request is known (e.g., "accept").
582-
* @return the value of the named general request property for this
583-
* connection. If key is null, then null is returned.
584-
* @throws IllegalStateException if already connected
585-
* @see #setRequestProperty(java.lang.String, java.lang.String)
586-
*/
587-
public String getRequestProperty(String key) {
588-
if (connected)
589-
throw new IllegalStateException("Already connected");
567+
requests.add(key, value);
568+
}
590569

591-
// if (requests == null)
592-
return null;
593570

594-
// return requests.findValue(key);
595-
}
571+
/**
572+
* Returns the value of the named general request property for this connection.
573+
*
574+
* @param key the keyword by which the request is known (e.g., "accept").
575+
* @return the value of the named general request property for this connection.
576+
* If key is null, then null is returned.
577+
* @throws IllegalStateException if already connected
578+
* @see #setRequestProperty(java.lang.String, java.lang.String)
579+
*/
580+
public String getRequestProperty(String key) {
581+
if (connected)
582+
throw new IllegalStateException("Already connected");
583+
if (requests == null)
584+
return null;
585+
return requests.findValue(key);
586+
}
596587

597-
/**
598-
* Returns an unmodifiable Map of general request
599-
* properties for this connection. The Map keys
600-
* are Strings that represent the request-header
601-
* field names. Each Map value is a unmodifiable List
602-
* of Strings that represents the corresponding
603-
* field values.
604-
*
605-
* @return a Map of the general request properties for this connection.
606-
* @throws IllegalStateException if already connected
607-
* @since 1.4
608-
*/
609-
public Map<String,List<String>> getRequestProperties() {
610-
if (connected)
611-
throw new IllegalStateException("Already connected");
588+
/**
589+
* Returns an unmodifiable Map of general request properties for this
590+
* connection. The Map keys are Strings that represent the request-header field
591+
* names. Each Map value is a unmodifiable List of Strings that represents the
592+
* corresponding field values.
593+
*
594+
* @return a Map of the general request properties for this connection.
595+
* @throws IllegalStateException if already connected
596+
* @since 1.4
597+
*/
598+
public Map<String, List<String>> getRequestProperties() {
599+
if (connected)
600+
throw new IllegalStateException("Already connected");
612601

613-
// if (requests == null)
614-
return Collections.EMPTY_MAP;
602+
if (requests == null)
603+
return Collections.EMPTY_MAP;
615604

616-
// return requests.getHeaders(null);
617-
}
605+
return requests.getHeaders(null);
606+
}
618607

619608
/**
620609
* Sets the default value of a general request property. When a

0 commit comments

Comments
 (0)