Php Support

Jul 7, 2012 at 11:00 PM

ok so trying to embed this into one of my applications. everything works great except that Php does not work

i added wo80's httpserver.addons and followed instructions to get php support. however i either get a 403 forbidden file type or by adding the mime type to the code i get prompted to download the php file, or a third is there is no errors, but all it does in load a blank page and have the php source code viewable by using 'view source' in the browser. html documents and such all work fine, server is running, ect ...

i verified pathing to the php-cgi.exe, no go. Thought it might be a bit depth issue so i switched to a 32bit debug build but still no go. just getting 403 forbidden file type.

anyone have any ideas? or is there another solution to get php support?

Developer
Jul 8, 2012 at 8:45 AM
Edited Jul 8, 2012 at 8:49 AM

Go to CgiHandler.cs, line 96. There's a TODO. Change the line

response.Add(new StringHeader(name, val));
to
if (name.ToLowerInvariant() == "content-type")
{
    response.ContentType.Value = val;
}
else
{
    response.Add(new StringHeader(name, val));
}
and let me know if it solves the issue.

Jul 8, 2012 at 3:05 PM

no go. maybe i am doing something wrong, here is my server class (VB.NET). 

Imports System
Imports System.IO
Imports System.Net
Imports HttpServer
Imports HttpServer.Addons
Imports HttpServer.Addons.Resources
 
Public Class vwdWebServer
 
    Dim listner As HttpServer.HttpListener
    Dim _server As Server
    Dim _resource As FileResources
    Dim _router As DefaultIndexRouter
    Dim _fileModule As FileModule
    Dim dirModule As DirectoryModule
    Dim fcgi As FastCgiApp
    Dim fcgiModule As FastCgiModule
    Dim cgiModule As CgiModule
 
    Dim _parent As MainForm
    Dim port As Integer
    Dim rootPath As String
 
    Public Sub New(ByVal root As StringByVal p As Form)
 
        _parent = DirectCast(p, MainForm)
        port = My.Settings.webserverport
 
        Try
            rootPath = root
            listner = HttpServer.HttpListener.Create(IPAddress.Loopback, port)
            _server = New Server()
            _server.MaxContentSize = 1024
 
            _resource = New FileResources("/", root)
 
            _router = New DefaultIndexRouter()
            _router.DefaultIndexFiles.Add("default.html")
            _router.DefaultIndexFiles.Add("index.php")
            _router.ServeDefaultIndex = True
            _router.Resources.Add(_resource)
 
            _server.Add(_router)
 
            _fileModule = New FileModule()
            _fileModule.EnableCompression = True
            _fileModule.Resources.Add(_resource)
 
            _server.Add(_fileModule)
 
            dirModule = New DirectoryModule()
            dirModule.Resources.Add(_resource)
 
            _server.Add(dirModule)
 
            Dim phppath As String = Application.StartupPath & "\data\php"
            cgiModule = New CgiModule()
            cgiModule.Resources.Add(_resource)
            cgiModule.AddCgiApplication("php", phppath & "\php-cgi.exe")
 
            _server.Add(cgiModule)
 
            'fcgi = New FastCgiApp(phppath & "\\php-cgi.exe", "-b {endpoint}", New IPEndPoint(IPAddress.Any, 8129))
 
            'fcgiModule = New FastCgiModule()
            'fcgiModule.Resources.Add(_resource)
            'fcgiModule.AddFastCgiApplication("php", fcgi)
 
            '_server.Add(fcgiModule)
            _server.Add(listner)
 
            StartServer()
        Catch ex As Exception
            _parent.appconsole.consolertf.AppendText("Error Starting Development Server: " & ex.Message & vbNewLine)
        End Try
 
    End Sub
 
    Public Sub StartServer()
        If (_server IsNot NothingThen
            _server.Start(15)
            _parent.appconsole.consolertf.AppendText("Development Server Started at: http://" & listner.Address.ToString & ":" & port & ", On Path: " & rootPath & vbNewLine)
        End If
    End Sub
 
    Public Sub StopServer()
        If (_server IsNot NothingThen
            _server.Stop(True)
            _parent.appconsole.consolertf.AppendText("Development Server Has been Stopped" & vbNewLine)
        End If
    End Sub
End Class
Developer
Jul 8, 2012 at 6:16 PM

Ok, my stupid fault. In HttpServer.Addons, FileModule.cs, line 138, change

if (!ContentTypes.TryGetValue(fileExtension, out header))
    throw new ForbiddenException("Forbidden file type: " + fileExtension);

to

if (!ContentTypes.TryGetValue(fileExtension, out header))
    return ProcessingResult.Continue;
This should be it ...

Jul 8, 2012 at 6:30 PM

brilliant!! now it works. Now i am getting the response (or one of them) that i was expecting (which is a php error). Brilliant mate. Thanks a ton.

oh and one more question, is mysql support possible with this addon?

Developer
Jul 8, 2012 at 7:06 PM

Using mysql from php should be no problem. It's a matter of configuring php right and doesn't affect the webserver itself.

Jul 8, 2012 at 7:21 PM

good to know. thank you for your help mate. :) mez iz a happy developer now :P

Jul 8, 2012 at 8:37 PM

sorry for being a pain, but another thing i have found with php support.

does this not support things like CodeIgniter Php Framework or the like? (IE: Zend, FuelPHP, ect ...)

because with CI for example it has a permitted_uri_chars config item, when this has any value (ie, default is: a-z 0-9~%.:_\-) a server url of: 127.0.0.1:8085 produces invalid uri characters, leaving this empty produces a 404 error, while on IIS for example it works perfectly no matter what this variable has in it. should i be using one of the MVC servers in the framework?I don't see why though, it should work should it not?

Developer
Jul 8, 2012 at 10:07 PM
Edited Jul 8, 2012 at 10:39 PM

You won't need the webserver mvc stuff to use php frameworks.

I haven't worked with CodeIgniter, but it seems you might have to add more allowed characters. Try adding a directory seperator 'a-z 0-9~%.:_\-\\' or disable the filtering completly to see if it works.

EDIT: Just watched the video tutorial. I think the webserver won't understand CodeIgniters way of handling URIs. Having an uri like 'http://127.0.0.1:8080/index.php/welcome/' the server will always look for a directory and won't load the index.php with appropriate path info. But it should be easy enough to write a router for this. Though I'm not sure if that solves the problem, it would be a start ...

EDIT 2: I think the cgi module should take care of that. Will work on it next week.

Jul 8, 2012 at 10:58 PM

ok so that little addition for dir seperator cured that issue, but still having 404.

 

for CI the url's are structured like:

example.com/index.php/controller/function/param/param/param (when mod_rewrite is not used in a .htaccess or iis equiv) or

example.com/controller/function/param/param/param (with mod_rewrite or iis equiv)

so using : 127.0.0.1:8085 or: 127.0.0.1:8085/index.php produces the CI 404 error page but using: 127.0.0.1:8085/index.php/home (which should show the CI default welcome page through the controller: welcome) produces the HttpServer.HttpException 404 error. what it is doing i believe is looking for a default document under directory path: index.php/home
and not actually passing the routing control to CI like it should.

Developer
Jul 9, 2012 at 5:55 AM

New version online: HttpServer.Addons.zip

Jul 9, 2012 at 4:01 PM

:P that is superb. Works a treat now. Awesome work. :)