Twelve47
Twelve47

Reputation: 3984

Using FastCGI applications from within a C# app

I'm developing a small webserver in C# as part of a larger project (the nature of the project prevents me from using something like apache nginx, which would be my first choice).

The webserver needs PHP to process some of the requests it recieves.

At the moment I'm running php as a cgi using System.Diagnostics.Process and piping data to and from. This works but is pretty slow (presumably from the overhead from PHP start starting from scratch, is the main issue). So I want to try using FastCGI instead.

I've looked at the FastCGI spec, and made a start at implementing a basic subset, but haven't have much luck. Most of of the examples I've seen have been libraries for developing FastCGI modules, not for invoking them, so I've had very little to use as reference.

Has any one got any experience of doing this under .NET, or could recommend any useful resources for this kind of project?

Upvotes: 10

Views: 1886

Answers (2)

Barkermn01
Barkermn01

Reputation: 6822

I have had to do the same thing its really easy to do,

If you just run the php-cgi exe with cmd and a test php file you will see it output

C:\Program Files (x86)\PHP>php-cgi c:/xampp/php/test.php
X-Powered-By: PHP/5.3.6
location test.php
Content-type: text/html

relocated

That is my example above all you need to do is call php-cgi and read in the response

The first double break is where your headers stop and your output occurs so if you have your console output in a variable E.G public string phpOutput then you just need a regex to split it on \n\n but set the count to 1 so it only splits on the first occurrence of \n\n,

E.G

Regex.Split(phpOutput, "(\r\n){2,}|\n{2,}|\r{2,}", RegexOptions.ExplicitCapture);

Upvotes: 0

Richard Harrison
Richard Harrison

Reputation: 19393

I also had to do something similar (during a transition period) and used MiniHttpd: an HTTP web server library. What I had to do was slightly different as I didn't need an httpd rather a way of executing PHP from a C# application.

It basically references the unmanaged code straight out of the DLL using the file on disk (see PhpAppDirectory.cs).

Upvotes: 2

Related Questions