How to react conditionally based on request when using netcat

I am trying to configure a web server using only batch scripts from Windows.

I already came up with the following script:

@echo off @setlocal enabledelayedexpansion for /l %%a in (1,0,2) do ( type tempfile.txt | nc -w 1 -l -p 80 | findstr mystring if !ERRORLEVEL! == 0 ( echo found > tempfile.txt ) else ( echo not-found > tempfile.txt ) ) 

However, the answer is always one request, I mean, if I enter something like this into the browser:

 REQUEST: localhost/mystring 

I will get the following answer:

 RESPONSE: not-found 

Only in the next request do I get the correct answer for the request presented above.

This is because as soon as netcat receives the request, it responds with the current contents of tempfile.txt, which is not yet updated based on the request.

Is there a way to block the response until tempfile.txt or any other method that achieves the expected result is updated?

+7
windows webserver named-pipes batch-file netcat
source share
2 answers

The problem is, as far as I can tell, nc cannot call back to configure its output based on client input. As soon as you...

 stdout generation | nc -l 

... blocking and waiting for a connection, its output has already been determined. This output is static at this point.

The only workaround that happens to me is quite inefficient. It mainly includes the following logic:

  • Listen for the connection prepared so that the client reboots.
  • Clear the GET address from previous request headers.
  • Submit relevant content to the client’s second connection.

Code example:

 @echo off & setlocal rem // macro for netcat command line and args set "nc=\cygwin64\bin\nc.exe -w 1 -l 80" rem // macro for sending refresh header set "refresh=^(echo HTTP/1.1 200 OK^&echo Refresh:0;^)^| %nc%" for /L %%# in (1,0,2) do ( rem // run refresh macro and capture client requested URL for /f "tokens=2" %%I in ('%refresh% ^| findstr "^GET"') do set "URL=%%I" rem // serve content to the client setlocal enabledelayedexpansion echo URL: !URL! | %nc% endlocal ) 

As a side note, slow expansion can distort the values ​​of variables set with exclamation points if it is enabled during installation. It is best to wait to enable delayed expansion before extraction.

In addition, when performing a logical check on %ERRORLEVEL% more graceful to use conditional execution . But this has nothing to do with my decision. :)

And finally, instead of type filename.html | nc -l type filename.html | nc -l , use <filename.html nc -l (or nc -l <filename.html ) to avoid the useless use of type .

+3
source share

Checkout -e , you can write a script that does the processing, and then execute

 nc -L -w1 -p 80 -eexec.bat 

which will broadcast stdin and stdout back and forth from nc to script as you want.

exec.bat may be something like (a few pseudocode):

 findstr mystring if not errorlevel 1 (echo found) else (echo not-found) 

or maybe a loop (also some pseudo-code):

 :top set /p input= if input wasn't "" echo %input% >> output.dat && goto top findstr /C:"mystring" output.dat if not errorlevel 1 (echo found) else (echo not-found) 
+5
source share

All Articles