FastCgiNet
FastCgiNet copied to clipboard
Example with PHP?
I'm trying to use this library with php-cgi.exe but i have no success.
I've launched a php-cgi istance with this .bat inside php folder:
@ECHO OFF
ECHO Starting PHP FastCGI...
php-cgi.exe -b 127.0.0.1:9123 -c C:\Program Files\PHP\php.ini
then using this code
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(new IPEndPoint(IPAddress.Loopback, 9123));
// There must be no two concurrent requests with the same requestid, even if in different sockets. For simplicity, this request will have request id equal to 1
ushort requestId = 1;
using (var request = new WebServerSocketRequest(sock, requestId))
{
// The BeginRequest Record defines how the application should respond. To know more read FastCgi's docs.
request.SendBeginRequest(Role.Responder, false);
// If there is any request body, send it through the Stdin stream. If there is nothing to send, send an End-Of-Request Record (an empty record)
request.SendEmptyStdin();
// At this point, the application is processing the request and cooking up a response for us, so let's welcome the incoming data until the response is over
int bytesRead;
byte[] buf = new byte[4096];
while (!request.ResponseComplete)
{
bytesRead = sock.Receive(buf, SocketFlags.None);
request.FeedBytes(buf, 0, bytesRead);
}
// All the application's response will be in the Stdout and/or Stderr streams
// Don't forget that the very first line of the output is ASCII encoded text with the response status, such as "Status: 200 OK"
using (var reader = new StreamReader(request.Stdout))
{
Console.Write(reader.ReadToEnd());
}
}
i've receive this: System.Net.Sockets.SocketException: 'An established connection was aborted by the software in your host machine
It seems that pgp-cgi is closing the connection abruptly? It's hard to say for sure without knowing a bit more. Do you know at which point in the code the exception is thrown? Can you get logs from php-cgi to better understand what's happening?
No, unluckily i don't have the source. I've downloaded here https://windows.php.net/download#php-7.3 but with iis there aren't any error using php-cgi.exe with fastcgi module. https://docs.microsoft.com/en-us/iis/application-frameworks/install-and-configure-php-applications-on-iis/using-fastcgi-to-host-php-applications-on-iis
I've never used php-cgi before, but it seems to me it acts as a FastCGI application. In that case, I think it might need important parameters to know which php file to run for a given request, which seems to be missing from the sample code you've pasted. In the README.md file there's a section of code like the one below, and it's executed before request.SendEmptyStdin()
:
// The Request Headers are sent with Params Records. You don't have to worry about the mechanisms, though: just write to the Params stream.
using (var nvpWriter = new NvpWriter(request.Params))
{
// The WriteParamsFromUri is a helper method that writes the following Name-Value Pairs:
// HTTP_HOST, HTTPS, SCRIPT_NAME, DOCUMENT_URI, REQUEST_METHOD, SERVER_NAME, QUERY_STRING, REQUEST_URI, SERVER_PROTOCOL, GATEWAY_INTERFACE
nvpWriter.WriteParamsFromUri(requestedUrl, requestMethod);
// The other http request headers, e.g. User-Agent
nvpWriter.Write("HTTP_USER_AGENT", "Super cool Browser v1.0");
}
Could you try adding and this section to your code (and adapt it to your needs) and see if that works?
Tried but got same error: System.Net.Sockets.SocketException: 'An established connection was aborted by the software in your host machine.'
I made an archive with the solution, if you want to try, launch "Launch_PHPCGI.bat" then go to fastcgi open solution and play, you'll can see the exception, here the archive http://static.doweb.site/phpfastcgi.zip
Thank you
Sorry for the delay, bertasoft,
I tried downloading the zip file you attached, but I no longer have a Windows machine, so it's hard for me to test this at all.
Looking at https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/, it seems that php-cgi might require other parameters that nvpWriter.WriteParamsFromUri
does not include, such as SCRIPT_FILENAME
. So I'd personally try a few things:
- The requested Url is http://github.com, but that will set things like
HTTP_HOST
to "github.com". I'm not sure how php-cgi might handle this, so I'd try checking that. - Also, I'd add a
nvpWriter.Write("SCRIPT_FILENAME", somethingHere);
. It's not clear to me what exactly is expected for this parameter, but it seems according to nginx's docs to be similar to the absolute path of the requested URL preceded by the document root? I'd try a few variations.
I tried but no success, then i tried with this https://github.com/LukasBoersma/FastCGI/issues/6 and goes successfully, I think there are something that doesn't want to go in your library (i don't know how to use) . Thank you very much for the support.