Advanced Http protocol

Posted by SkyRanger on Sat, 21 Dec 2019 16:03:15 +0100

What is http protocol

http protocol: Format Specification for data transmission between browser client and server

Tools for viewing the http protocol

1) Use Firefox's firebug plug-in (right click > firebug > Network)
2) Use Google's "censorship element"

http protocol content

Request (browser->Server)
GET /day09/hello HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Response (server browser)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 24
Date: Fri, 30 Jan 2015 01:54:57 GMT

this is hello servlet!!!

Http request

GET /day09/hello HTTP/1.1               -Request line
Host: localhost:8080                    --Request headers (multiple key-value Object)
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
                                    --A blank line
name=eric&password=123456             --(Optional) entity content

Request line

	GET /day09/hello HTTP/1.1     

http protocol version

http1.0: after the current browser client establishes a connection with the server, it can only send a request once, and the connection is closed after a request.
http1.1: after the current browser client establishes a connection with the server, it can send multiple requests in one connection. (basically use 1.1)

Request resources

URL: uniform resource locator. http://localhost:8080/day09/testImg.html. Only Internet resources can be located. Is a subset of URI s.
URI: uniform resource identifier. /day09/hello. Used to tag any resource. It can be the resource of local file system and local area network( //192.168.14.10/myweb/index.html can be the Internet.

Common request methods

GET , POST, HEAD, TRACE, PUT, CONNECT

Prevent illegal links (referers)

	<filter>
		<filter-name>ImgFilter</filter-name>
		<filter-class>com.itmayiedu.filter.ImgFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>ImgFilter</filter-name>
		<url-pattern>/static/*</url-pattern>
	</filter-mapping>
public class ImgFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("initialization...");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("doFilter....");
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		//Get source in request header
		String referer = req.getHeader("referer");
		//Get current request name
		String serverName = request.getServerName();
		System.out.println("referer:"+referer+"----serverName:"+serverName+":"+serverName);
	    if(referer==null||(!referer.contains(serverName))){
	    	req.getRequestDispatcher("/imgs/error.png").forward(req, res);
	    	return ;
	    }
		chain.doFilter(req, res);
	}

	public void destroy() {

	}

}

Case - Request redirection (Location)

Topics: xml Firefox network Windows