I am trying to set up a web server using only windows batch scripting.
I have already come 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 response is always one request behind, I mean, if I type something like this into the browser:
REQUEST: localhost/mystring
I will get the following response:
RESPONSE: not-found
Only in the next request I will receive the correct answer for the request presented above.
This is happening because as soon as netcat receives a request it responds with the current content of the tempfile.txt which has not been updated yet based on the request.
Is there any way to block the response until the tempfile.txt is updated or any other method which accomplish the expected result?
tempfile.txt
with either the string "found" or "not-found"; your code shouldn't be working at all. – Plantaineaternc
TCP output conditional based on the browser's request header content. – Miscallfindstr
executes. Netcat couldn't care less about what happens totempfile.txt
until the next loop iteration. – Miscalltype
openstempfile.txt
for reading and sends its contents to stdout, then closes the file handle.netcat
takes the piped stdout data and prepares to send it to the next client that connects to TCP port 80. When a client connects,netcat
vomits the browser request headers to the console and responds with its prepared piped-in payload, then its thread terminates.find
sifts through the vomit for the requested URL. Then some logic happens, and the loop repeats. Onlynetcat
doesn't work the way OP reckons it ought to. – Miscall