http.lex
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:5k
- package net.wastl.webmail.standalone;
- import java_cup.runtime.*; /* this is convenience, but not necessary */
- import java.io.Reader;
- /*
- * http.lex
- *
- * Created: April 1999
- *
- * Copyright (C) 1999-2000 Sebastian Schaffert
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- %%
- %class HTTPLexer
- %extends sym
- %{
- boolean has_content=false;
- boolean versionset=false;
- boolean set_contenttype=false;
- boolean set_contentlength=false;
- String content_type;
- int contentlength=0;
- int cur_contentlength;
- StringBuffer string = new StringBuffer();
- private Symbol symbol(int type) {
- return new Symbol(type, yyline, yycolumn);
- }
- private Symbol symbol(int type, Object value) {
- return new Symbol(type, yyline, yycolumn, value);
- }
-
- public String getContentType() {
- return content_type;
- }
- %}
- %8bit
- %column
- %line
- %caseless
- %state HEADER
- %state HEADERDATA
- %state END
- %state CONTENT
- //%cup
- %type java_cup.runtime.Symbol
- %eofval{
- return new java_cup.runtime.Symbol(sym.EOF);
- %eofval}
- DIGIT=[0-9]
- HEX = "A" | "B" | "C" | "D" | "E" | "F" | "a" | "b" | "c" | "d" | "e" | "f" | DIGIT
- CR=r
- LF=n
- SP=" "
- HT=t
- CRLF={CR} {LF} | {CR} | {LF}
- LWS={CRLF} ({SP}|{HT})+
- METHOD="GET" | "POST" | "HEAD" | "PUT" | "OPTION"
- VERSION="HTTP" "/" {DIGIT}+ "." {DIGIT}+
- HEAD_SEP=({SP}|{HT})* ":" ({SP}|{HT})*
- TOKEN=[^()<>@,;:\">/[]?={}t 00- 32127" "]
- ANY=[ 32-255]
- NATIONAL=[128-255]
- PATH=":" | "@" | "&" | "=" | "+" | [A-Za-z0-9] | "%" {HEX} {HEX} | ";" | "/" | "?" | ":" | "!" | "*" | "'" | "(" | ")" | "," | "$" | "-" | "_" | "." | "%" | "|"
- URI={TOKEN}+ "://" {PATH}+ | "/" {PATH}*
- BOUND="multipart/form-data; boundary="
- URLENC="application/x-www-form-urlencoded"
- %%
- /* Parsing the first line that has to contain Method, URI and HTTP-Version */
- <YYINITIAL> {
- ({SP}|{HT})+ {
- return symbol(SP);
- }
- {CRLF} {
- if(versionset) {
- yybegin(HEADER);
- return symbol(CRLF);
- }
- }
- {METHOD} {
- versionset=true;
- return symbol(METHOD,yytext());
- }
- {URI} {
- return symbol(URI,yytext());
- }
- {VERSION} {
- return symbol(VERSION,yytext().substring(5));
- }
- }
- <HEADER> {
- {CRLF} {
- if(has_content) {
- cur_contentlength=0;
- string.setLength(0);
- yybegin(CONTENT);
- } else {
- yy_atEOF = true; /* indicate end of file */
- yy_endRead = yy_startRead; /* invalidate buffer */
- yybegin(END);
- }
- return symbol(CRLF);
- }
- {HEAD_SEP} {
- string.setLength(0);
- yybegin(HEADERDATA);
- return symbol(HEAD_SEP);
- }
- ({SP}|{HT})+ {
- return symbol(SP);
- }
- {TOKEN}+ / ":" {
- if(yytext().toUpperCase().trim().equals("CONTENT-LENGTH")) {
- set_contentlength=true;
- } else if(yytext().toUpperCase().trim().equals("CONTENT-TYPE")) {
- set_contenttype=true;
- }
- return symbol(TOKEN,yytext());
- }
- }
- <HEADERDATA> {
- {BOUND} {TOKEN}+ | {URLENC} {TOKEN}* {
- has_content=true;
- string.append(yytext());
- }
- {ANY}+ {
- string.append(yytext());
- }
- {LWS} {
- string.append(" ");
- }
- {CRLF} {
- if(set_contentlength) {
- try {
- contentlength=Integer.parseInt(string.toString());
- } catch(NumberFormatException e) {
- e.printStackTrace();
- }
- set_contentlength=false;
- } else if(set_contenttype) {
- content_type=string.toString();
- set_contenttype=false;
- }
- yybegin(HEADER);
- return symbol(DATA,string.toString());
- }
- }
- <CONTENT> {
- .|n {
- string.append(yytext());
- cur_contentlength++;
- if(contentlength>0 && cur_contentlength >= contentlength) {
- yy_atEOF = true; /* indicate end of file */
- yy_endRead = yy_startRead; /* invalidate buffer */
- yybegin(END);
- return symbol(DATA,string.toString());
- }
- }
- }
- <END> {
- .|n { return symbol(EOF); }
- }
- /* error fallback */
- .|n { System.err.println("Invalid input!"); }