view src/luan/webserver/Response.java @ 1137:c123ee15f99b

add webserver
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 29 Jan 2018 18:49:59 -0700
parents
children 2dda3c92a473
line wrap: on
line source

package luan.webserver;

import java.io.InputStream;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Collections;


public class Response {
	public final String protocol = "HTTP/1.1";
	public volatile Status status = Status.OK;
	public final Map<String,String> headers = Collections.synchronizedMap(new LinkedHashMap<String,String>());
	{
		headers.put("Server","ThreeBody");
	}
	public volatile Body body;

	public static class Body {
		public final long length;
		public final InputStream content;
	
		public Body(long length,InputStream content) {
			this.length = length;
			this.content = content;
		}
	}


	public String toHeaderString() {
		StringBuilder sb = new StringBuilder();
		sb.append( protocol )
			.append( ' ' ).append( status.code )
			.append( ' ' ).append( status.reason )
			.append( "\r\n" )
		;
		for( Map.Entry<String,String> entry : headers.entrySet() ) {
			String name = entry.getKey();
			String value = entry.getValue();
			sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
		}
		sb.append( "\r\n" );
		return sb.toString();
	}
}