Hacker News new | ask | show | jobs
Making HTTP clients for use with netcat-like programs, part 2 of 2
2 points by textmode 1860 days ago

      # make new http client (yy025)
      # hh2 is an #include in 025.l below
      yy018 < ua.txt > hh2;
      flex -8iCrfa 025.l;
      # cpp -P 025.l|less;
      cc -Wall -std=c89 -static -o yy025 lex.yy.c;

   save as "025.l":

        int yy_get_next_buffer();
        int fileno(FILE *);
        int setenv (const char *, const char *, int);
        int dprintf(int, const char *__restrict, ...);
        #define httpMethod "GET"
        #define httpVersion "1.1"
        #define Host ""
        #define jmp BEGIN
        #define Y(x,y) fprintf(stdout,x,y)
        int count,path,ka;
        int httpheaders(){
          setenv("httpMethod",httpMethod,0);Y("%s ",getenv("httpMethod"));
          Y("%s HTTP/",getenv("Path"));
          setenv("httpVersion",httpVersion,0);Y("%s\r\n",getenv("httpVersion"));
          #include "hh2"                                                                                                                         
          fputs("\r\n",stdout);
          return 0;}
        int keepaliveclose(){setenv("Connection","close",1);return 0;}
     %option nounput 
     %s xa xb xc
     xa "http://"|"https://"
     xb [-A-Za-z0-9.:]*
     xc [^#'|<> \r\n]*
     %%
     ^{xa} count++;setenv("Scheme",yytext,0);jmp xa;
     <xa>{xb} setenv("Host",yytext,1);if(!getenv("Host"))setenv("Host",Host,0);jmp xb;
     <xb>\n path=0;setenv("Path","/",0);httpheaders();jmp 0;
     <xb>{xc} path=1;setenv("Path",yytext,1);httpheaders();jmp 0;
     .|\n
     %%
            int main(){input();yylex();if(!getenv("nokac")){if(count>1){keepaliveclose();httpheaders();};}return 0;}
            int yywrap(){dprintf(6,"%s",getenv("Host"));return 0;}
1 comments

Corrections:

/int main/{s/input();//;s/return 0/exit(0)/;};/int yywrap/s/return 0/exit(0)/;/%option/s/$/ noinput/