http.lex
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:5k
源码类别:

WEB邮件程序

开发平台:

C/C++

  1. package net.wastl.webmail.standalone;
  2. import java_cup.runtime.*;   /* this is convenience, but not necessary */
  3. import java.io.Reader;
  4. /*
  5.  * http.lex
  6.  *
  7.  * Created: April 1999
  8.  *
  9.  * Copyright (C) 1999-2000 Sebastian Schaffert
  10.  * 
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version 2
  14.  * of the License, or (at your option) any later version.
  15.  * 
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  */
  25. %%
  26. %class HTTPLexer
  27. %extends sym
  28. %{
  29.     boolean has_content=false;
  30.     boolean versionset=false;
  31.     boolean set_contenttype=false;
  32.     boolean set_contentlength=false;
  33.     String content_type;
  34.     int contentlength=0;
  35.     int cur_contentlength;
  36.     StringBuffer string = new StringBuffer();
  37.     private Symbol symbol(int type) {
  38. return new Symbol(type, yyline, yycolumn);
  39.     }
  40.     private Symbol symbol(int type, Object value) {
  41. return new Symbol(type, yyline, yycolumn, value);
  42.     }
  43.   
  44.     public String getContentType() {
  45. return content_type;
  46.     }
  47. %}
  48. %8bit
  49. %column
  50. %line
  51. %caseless
  52. %state HEADER
  53. %state HEADERDATA
  54. %state END
  55. %state CONTENT
  56.     //%cup
  57. %type java_cup.runtime.Symbol
  58. %eofval{
  59.     return new java_cup.runtime.Symbol(sym.EOF);
  60. %eofval}
  61. DIGIT=[0-9]
  62. HEX = "A" | "B" | "C" | "D" | "E" | "F" | "a" | "b" | "c" | "d" | "e" | "f" | DIGIT
  63. CR=r
  64. LF=n
  65. SP=" "
  66. HT=t
  67. CRLF={CR} {LF} | {CR} | {LF} 
  68. LWS={CRLF} ({SP}|{HT})+
  69. METHOD="GET" | "POST" | "HEAD" | "PUT" | "OPTION"
  70. VERSION="HTTP" "/" {DIGIT}+ "." {DIGIT}+
  71. HEAD_SEP=({SP}|{HT})* ":" ({SP}|{HT})*
  72. TOKEN=[^()<>@,;:\">/[]?={}t00-32127" "]
  73. ANY=[32-255]
  74. NATIONAL=[128-255]
  75. PATH=":" | "@" | "&" | "=" | "+" | [A-Za-z0-9] | "%" {HEX} {HEX} | ";" | "/" | "?" | ":" | "!" | "*" | "'" | "(" | ")" | "," | "$" | "-" | "_" | "." | "%" | "|"
  76. URI={TOKEN}+ "://" {PATH}+ | "/" {PATH}*
  77. BOUND="multipart/form-data; boundary="
  78. URLENC="application/x-www-form-urlencoded"
  79. %%
  80. /* Parsing the first line that has to contain Method, URI and HTTP-Version */
  81. <YYINITIAL> {
  82.   ({SP}|{HT})+     { 
  83.                       return symbol(SP); 
  84.                    }
  85.   {CRLF}           { 
  86.                       if(versionset) {
  87.                         yybegin(HEADER); 
  88.         return symbol(CRLF);
  89.                       }
  90.                    }
  91.   {METHOD}         { 
  92.                       versionset=true;
  93.                       return symbol(METHOD,yytext()); 
  94.                    }
  95.   {URI}            { 
  96.                       return symbol(URI,yytext()); 
  97.                    }
  98.   {VERSION}        { 
  99.                       return symbol(VERSION,yytext().substring(5)); 
  100.                    }
  101. }
  102. <HEADER> {
  103.   {CRLF}           {
  104.                      if(has_content) {
  105.  cur_contentlength=0;
  106.  string.setLength(0); 
  107.  yybegin(CONTENT);
  108.                     } else {
  109. yy_atEOF = true;            /* indicate end of file */
  110. yy_endRead = yy_startRead;  /* invalidate buffer    */
  111. yybegin(END);
  112.     }
  113.      return symbol(CRLF);
  114.                    }
  115.   {HEAD_SEP}       { 
  116.                      string.setLength(0); 
  117.                      yybegin(HEADERDATA); 
  118.      return symbol(HEAD_SEP);
  119.                    }
  120.   ({SP}|{HT})+     { 
  121.                      return symbol(SP); 
  122.                    }
  123.   {TOKEN}+ / ":"   { 
  124.      if(yytext().toUpperCase().trim().equals("CONTENT-LENGTH")) {
  125.  set_contentlength=true;
  126.      } else if(yytext().toUpperCase().trim().equals("CONTENT-TYPE")) {
  127.  set_contenttype=true;
  128.      }
  129.                      return symbol(TOKEN,yytext()); 
  130.                    }
  131. }
  132. <HEADERDATA> {
  133.   {BOUND} {TOKEN}+ |  {URLENC} {TOKEN}* { 
  134.                      has_content=true;
  135.      string.append(yytext());
  136.                    }
  137.   {ANY}+           {  
  138.      string.append(yytext());
  139.                    }
  140.   {LWS}            { 
  141.      string.append(" ");
  142.                    }
  143.   {CRLF}           { 
  144.      if(set_contentlength) {
  145.  try {
  146.      contentlength=Integer.parseInt(string.toString());
  147.  } catch(NumberFormatException e) {
  148.      e.printStackTrace();
  149.  }
  150.  set_contentlength=false;
  151.      } else if(set_contenttype) {
  152.  content_type=string.toString();
  153.  set_contenttype=false;
  154.      }
  155.      yybegin(HEADER);  
  156.                      return symbol(DATA,string.toString()); 
  157.                    }
  158. }
  159. <CONTENT> {
  160.   .|n            {  
  161.      string.append(yytext());
  162.      cur_contentlength++;
  163.      if(contentlength>0 && cur_contentlength >= contentlength) {
  164.        yy_atEOF = true;            /* indicate end of file */
  165.        yy_endRead = yy_startRead;  /* invalidate buffer    */
  166.        yybegin(END);  
  167.                        return symbol(DATA,string.toString()); 
  168.      }
  169.                    }
  170. }
  171. <END> {
  172.     .|n { return symbol(EOF); }
  173. }
  174. /* error fallback */
  175. .|n                             { System.err.println("Invalid input!"); }