25 26 /*- 27 * news stream opener 28 */ 29 30 package sun.net.www; 31 32 import java.io.*; 33 import java.util.Collections; 34 import java.util.*; 35 36 /** An RFC 844 or MIME message header. Includes methods 37 for parsing headers from incoming streams, fetching 38 values, setting values, and printing headers. 39 Key values of null are legal: they indicate lines in 40 the header that don't have a valid key, but do have 41 a value (this isn't legal according to the standard, 42 but lines like this are everywhere). */ 43 public 44 class MessageHeader { 45 private String keys[]; 46 private String values[]; 47 private int nkeys; 48 49 public MessageHeader () { 50 grow(); 51 } 52 53 public MessageHeader (InputStream is) throws java.io.IOException { 54 parseHeader(is); 55 } 56 57 /** 58 * Returns list of header names in a comma separated list 59 */ 60 public synchronized String getHeaderNamesInList() { 61 StringJoiner joiner = new StringJoiner(","); 62 for (int i=0; i<nkeys; i++) { 63 joiner.add(keys[i]); 64 } 65 return joiner.toString(); 66 } 428 c <= ' ')) { 429 len--; 430 substr = true; 431 } 432 return substr ? id.substring(st, len) : id; 433 } 434 435 /** Parse a MIME header from an input stream. */ 436 public void parseHeader(InputStream is) throws java.io.IOException { 437 synchronized (this) { 438 nkeys = 0; 439 } 440 mergeHeader(is); 441 } 442 443 /** Parse and merge a MIME header from an input stream. */ 444 @SuppressWarnings("fallthrough") 445 public void mergeHeader(InputStream is) throws java.io.IOException { 446 if (is == null) 447 return; 448 char s[] = new char[10]; 449 int firstc = is.read(); 450 while (firstc != '\n' && firstc != '\r' && firstc >= 0) { 451 int len = 0; 452 int keyend = -1; 453 int c; 454 boolean inKey = firstc > ' '; 455 s[len++] = (char) firstc; 456 parseloop:{ 457 while ((c = is.read()) >= 0) { 458 switch (c) { 459 case ':': 460 if (inKey && len > 0) 461 keyend = len; 462 inKey = false; 463 break; 464 case '\t': 465 c = ' '; 466 /*fall through*/ 467 case ' ': 468 inKey = false; 469 break; 470 case '\r': 471 case '\n': 472 firstc = is.read(); 473 if (c == '\r' && firstc == '\n') { 474 firstc = is.read(); 475 if (firstc == '\r') 476 firstc = is.read(); 477 } 478 if (firstc == '\n' || firstc == '\r' || firstc > ' ') 479 break parseloop; 480 /* continuation */ 481 c = ' '; 482 break; 483 } 484 if (len >= s.length) { 485 char ns[] = new char[s.length * 2]; 486 System.arraycopy(s, 0, ns, 0, len); 487 s = ns; 488 } 489 s[len++] = (char) c; 490 } 491 firstc = -1; 492 } 493 while (len > 0 && s[len - 1] <= ' ') 494 len--; 495 String k; 496 if (keyend <= 0) { 497 k = null; 498 keyend = 0; 499 } else { 500 k = String.copyValueOf(s, 0, keyend); 501 if (keyend < len && s[keyend] == ':') 502 keyend++; 503 while (keyend < len && s[keyend] <= ' ') 504 keyend++; 505 } | 25 26 /*- 27 * news stream opener 28 */ 29 30 package sun.net.www; 31 32 import java.io.*; 33 import java.util.Collections; 34 import java.util.*; 35 36 /** An RFC 844 or MIME message header. Includes methods 37 for parsing headers from incoming streams, fetching 38 values, setting values, and printing headers. 39 Key values of null are legal: they indicate lines in 40 the header that don't have a valid key, but do have 41 a value (this isn't legal according to the standard, 42 but lines like this are everywhere). */ 43 public 44 class MessageHeader { 45 private String[] keys; 46 private String[] values; 47 private int nkeys; 48 49 public MessageHeader () { 50 grow(); 51 } 52 53 public MessageHeader (InputStream is) throws java.io.IOException { 54 parseHeader(is); 55 } 56 57 /** 58 * Returns list of header names in a comma separated list 59 */ 60 public synchronized String getHeaderNamesInList() { 61 StringJoiner joiner = new StringJoiner(","); 62 for (int i=0; i<nkeys; i++) { 63 joiner.add(keys[i]); 64 } 65 return joiner.toString(); 66 } 428 c <= ' ')) { 429 len--; 430 substr = true; 431 } 432 return substr ? id.substring(st, len) : id; 433 } 434 435 /** Parse a MIME header from an input stream. */ 436 public void parseHeader(InputStream is) throws java.io.IOException { 437 synchronized (this) { 438 nkeys = 0; 439 } 440 mergeHeader(is); 441 } 442 443 /** Parse and merge a MIME header from an input stream. */ 444 @SuppressWarnings("fallthrough") 445 public void mergeHeader(InputStream is) throws java.io.IOException { 446 if (is == null) 447 return; 448 char[] s = new char[10]; 449 int firstc = is.read(); 450 while (firstc != '\n' && firstc != '\r' && firstc >= 0) { 451 int len = 0; 452 int keyend = -1; 453 int c; 454 boolean inKey = firstc > ' '; 455 s[len++] = (char) firstc; 456 parseloop:{ 457 while ((c = is.read()) >= 0) { 458 switch (c) { 459 case ':': 460 if (inKey && len > 0) 461 keyend = len; 462 inKey = false; 463 break; 464 case '\t': 465 c = ' '; 466 /*fall through*/ 467 case ' ': 468 inKey = false; 469 break; 470 case '\r': 471 case '\n': 472 firstc = is.read(); 473 if (c == '\r' && firstc == '\n') { 474 firstc = is.read(); 475 if (firstc == '\r') 476 firstc = is.read(); 477 } 478 if (firstc == '\n' || firstc == '\r' || firstc > ' ') 479 break parseloop; 480 /* continuation */ 481 c = ' '; 482 break; 483 } 484 if (len >= s.length) { 485 char[] ns = new char[s.length * 2]; 486 System.arraycopy(s, 0, ns, 0, len); 487 s = ns; 488 } 489 s[len++] = (char) c; 490 } 491 firstc = -1; 492 } 493 while (len > 0 && s[len - 1] <= ' ') 494 len--; 495 String k; 496 if (keyend <= 0) { 497 k = null; 498 keyend = 0; 499 } else { 500 k = String.copyValueOf(s, 0, keyend); 501 if (keyend < len && s[keyend] == ':') 502 keyend++; 503 while (keyend < len && s[keyend] <= ' ') 504 keyend++; 505 } |