FileDocCategorySizeDatePackage
HTTPUtils.javaAPI DocAzureus 3.0.3.46359Wed Sep 26 14:54:02 BST 2007com.aelitis.azureus.core.util

HTTPUtils

public class HTTPUtils extends Object

Fields Summary
public static final String
NL
private static final String
default_type
private static final Map
file_types
private static final Set
compression
Constructors Summary
Methods Summary
public static booleancanGZIP(java.lang.String accept_encoding)

		boolean	gzip_reply = false;
		
		if ( accept_encoding != null ){
			
			accept_encoding = accept_encoding.toLowerCase();
			
			int gzip_index = accept_encoding.indexOf( "gzip" );
			
			if ( gzip_index != -1 ){
				
				gzip_reply	= true;
				
				if ( accept_encoding.length() - gzip_index >= 8 ){
				
						// gzip;q=0
						// look to see if there's a q=0 (or 0.0) disabling gzip
	
					char[]	chars = accept_encoding.toCharArray();
					
					boolean	q_value = false;
													
					for (int i=gzip_index+4;i<chars.length;i++){
						
						char	c = chars[i];
						
						if ( c == '," ){
							
							break;
							
						}else if ( c == '=" ){
							
							q_value		= true;
							gzip_reply	= false;
							
						}else{
							
							if ( q_value ){
								
								if ( c != ' " && c != '0" && c != '." ){
									
									gzip_reply	= true;
									
									break;
								}
							}
						}
					}
				}
			}
		}
		
		return( gzip_reply );
	
public static java.io.InputStreamdecodeChunkedEncoding(java.io.InputStream is)

		String reply_header = "";

		while (true) {

			byte[] buffer = new byte[1];

			if (is.read(buffer) <= 0) {

				throw (new IOException("Premature end of input stream"));
			}

			reply_header += (char) buffer[0];

			if (reply_header.endsWith(NL + NL)) {

				break;
			}
		}

		int p1 = reply_header.indexOf(NL);

		String first_line = reply_header.substring(0, p1).trim();

		if (first_line.indexOf("200") == -1) {

			throw (new IOException("HTTP request failed:" + first_line));
		}

		String lc_reply_header = reply_header.toLowerCase();

		int te_pos = lc_reply_header.indexOf("transfer-encoding");

		if (te_pos != -1) {

			String property = lc_reply_header.substring(te_pos);

			property = property.substring(property.indexOf(':") + 1,
					property.indexOf(NL)).trim();

			if (property.equals("chunked")) {

				ByteArrayOutputStream baos = new ByteArrayOutputStream();

				String chunk = "";

				int total_length = 0;

				while (true) {

					int x = is.read();

					if (x == -1) {

						break;
					}

					chunk += (char) x;

					// second time around the chunk will be prefixed with NL
					// from end of previous
					// so make sure we ignore this

					if (chunk.endsWith(NL) && chunk.length() > 2) {

						int semi_pos = chunk.indexOf(';");

						if (semi_pos != -1) {

							chunk = chunk.substring(0, semi_pos);
						}

						chunk = chunk.trim();

						int chunk_length = Integer.parseInt(chunk, 16);

						if (chunk_length <= 0) {

							break;
						}

						total_length += chunk_length;

						if (total_length > 1024 * 1024) {

							throw (new IOException("Chunk size " + chunk_length
									+ " too large"));
						}

						byte[] buffer = new byte[chunk_length];

						int buffer_pos = 0;
						int rem = chunk_length;

						while (rem > 0) {

							int len = is.read(buffer, buffer_pos, rem);

							if (len <= 0) {

								throw (new IOException(
										"Premature end of stream"));
							}

							buffer_pos += len;
							rem -= len;
						}

						baos.write(buffer);

						chunk = "";
					}
				}

				return (new ByteArrayInputStream(baos.toByteArray()));
			}
		}

		return (is);
	
public static java.lang.StringguessContentTypeFromFileType(java.lang.String file_type)

param
file_type file extension
return
apropriate content type string if found


	 
		file_types.put("html", "text/html");
		file_types.put("htm", "text/html");
		file_types.put("css", "text/css");
		file_types.put("js", "text/javascript");
		file_types.put("xml", "text/xml");
		file_types.put("xsl", "text/xml");
		file_types.put("jpg", "image/jpeg");
		file_types.put("jpeg", "image/jpeg");
		file_types.put("gif", "image/gif");
		file_types.put("tiff", "image/tiff");
		file_types.put("bmp", "image/bmp");
		file_types.put("png", "image/png");
		file_types.put("torrent", "application/x-bittorrent");
		file_types.put("tor", "application/x-bittorrent");
		file_types.put("zip", "application/zip");
		file_types.put("txt", "text/plain");
		file_types.put("jar", "application/java-archive");
		file_types.put("jnlp", "application/x-java-jnlp-file");
		file_types.put("mp3", "audio/x-mpeg");
		file_types.put("flv", "video/x-flv");

		compression.add("text/html");
		compression.add("text/css");
		compression.add("text/xml");
		compression.add("text/plain");
		compression.add("text/javascript");
	
		if (file_type != null) {

			String type = (String) file_types.get(file_type.toLowerCase());

			if (type != null) {

				return (type);
			}
		}

		return (default_type);
	
public static booleanuseCompressionForFileType(java.lang.String file_type)

param
file_type a file type like text/plain
return
true if the file_type should be compressed

		return compression.contains(file_type);