My favorites | Sign in
Project Logo
                
Search
for
Updated Jun 10, 2009 by valenok
Labels: Featured
EmbeddingMongoose  
Covers embedding strategies, with examples

C/C++

End to end example of embedding the webserver into the application:

  • Copy mongoose.c, mongoose.h to your source code directory
  • Write code that starts and configures Mongoose in one of your C/C++ files. As an alternative, you can just take authentication.c and rename in to my_app.c
  • Build and run the program. On UNIX:
  • $ cc my_app.c mongoose.c -ldl -lpthread -o prog
    $ ./prog
  • Start your browser, type in URL http://127.0.0.1:8080/ . You must see the page printed by function login_page().

This example shows how to implement an authentication, and how to handle /login URI by an embedded function. Extend this example according to your needs.

You can refer to Mongoose unit test embed.c to see the how embedding is done there. The API is easy to understand and use, however, if you have questions, do not hesitate to drop a message to the mailing list, where all questions get answered.

Python

Since verion 2.6, Mongoose provides a wrapper module for Python. To use it on UNIX,

If you want to start from scratch, you can use the example file provided in the Mongoose source tree as a starting point. Here is a procedure:

  • Execute following commands to build a shared library and run the example
  • % svn checkout http://mongoose.googlecode.com/svn/trunk/ mongoose-read-only
    % cd mongoose-readonly
    % make linux     # Create_mongoose.so
    % LD_LIBRARY_PATH=. python ./bindings/python/example.py  # This starts server
  • Start your browser, go to http://localhost:8080/foo
  • Modify example.py according to your needs.

C#

Ruby

TODO

Dynamic loading - MS Visual Basic example

You can use Mongoose in any language that supports dynamic loading. You need the Mongoose Dynamically Linked Library (DLL) built. On Windows platform, it comes preinstalled as mongoose.dll in the installation directory. Below is an example on how do you use Mongoose from the Visual Basic program:

Imports System.Runtime.InteropServices

Module Module1

    ' Mongoose API as defined in mongoose.h header file, http://mongoose.googlecode.com/svn/trunk/mongoose.h
    '

    <StructLayout(LayoutKind.Sequential, Pack:=1)> Structure mg_http_header
        Public name As String
        Public value As String
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=1)> Structure mg_request_info
        Public request_method As String
        Public uri As String
        Public query_string As String
        Public post_data As String
        Public remote_user As String
        Public remote_ip As Integer
        Public remote_port As Integer
        Public post_data_len As Integer
        Public http_version_minor As Integer
        Public http_version_major As Integer
        Public status_code As Integer
        Public num_headers As Integer
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=64)> Public http_headers() As mg_http_header
    End Structure

    Declare Function mg_start Lib "C:\mongoose.dll" () As IntPtr
    Declare Function mg_set_option Lib "C:\mongoose.dll" (ByVal ctx As IntPtr, ByVal opt_name As String, ByVal opt_value As String) As Integer
    Declare Sub mg_bind_to_uri Lib "C:\mongoose.dll" (ByVal ctx As IntPtr, ByVal uri_regex As String, ByVal func As mg_callback_t)
    Declare Function mg_write Lib "C:\mongoose.dll" (ByVal conn As IntPtr, ByVal fmt As String, ByVal len As Integer) As Integer

    Delegate Sub mg_callback_t(ByVal ctx As IntPtr, ByRef request_info As mg_request_info)

    ' Now goes VB code that uses the API. This function gets called when "/vb" URI is requested
    '
    Sub UriHandler(ByVal conn As IntPtr, ByRef request_info As mg_request_info)
        Dim output As String, i As Integer
        output = "HTTP/1.1 200 OK" & vbCrLf & vbCrLf & "Hello from VB!" & vbCr
        output &= "Method: " & request_info.request_method & vbCr
        output &= "URI: " & request_info.uri & vbCr
        output &= "HTTP headers: " & vbCr
        For i = 0 To request_info.num_headers - 1
            output &= "  " & request_info.http_headers(i).name & ": " & request_info.http_headers(i).value & vbCr
        Next i
        mg_write(conn, output, output.Length)
    End Sub

    ' Create web server instance, set options, and fall to sleep. Web server continues to execute in another thread, serving HTTP requests
    '
 
    Sub Main()
        Dim ctx As IntPtr
        ctx = mg_start()
        mg_set_option(ctx, "ports", "9090")
        mg_set_option(ctx, "root", "C:\\")
        mg_bind_to_uri(ctx, "/vb", AddressOf UriHandler)
        Threading.Thread.Sleep(9999999)
    End Sub

End Module

Copy/paste the code in your Visual Basic IDE, copy mongoose.dll to the c:\ directory, run VB program. Start you browser, go to http://127.0.0.1:9090 . You must see the content of your disk c:\. Go to http://127.0.0.1:9090/vb , you must see "Hello from VB" message.


Comment by zongliang.li, Mar 04, 2009

Could you add the HTTP Post upload as a built-in function of mongoose?

Thank you very much

gnozil AT gmail DOT com

Comment by yannguernion, May 17, 2009

Hi,

Do you have any plan to add SAPI support for binding the web server with PHP ? Would be much more efficient than CGI, and mongoose would be a serious (light & compact) alternative to LAMP PHP application server. A must for releasing small web based applications.

Thx

Comment by valenok, May 17, 2009

SAPI support sounds attractive, but I have following concerns:

  1. To compete with existing solutions, I have to build and maintain a whole bundle that includes PHP, MySQL etcetera. Since I am the only developer, it is unrealistic that I can do that together with the development properly.
2. There are already LAMP solutions out there, the niche is taken. Mongoose occupies a bit different niche - implementing web interface for the applications, i.e. embedding web server. Standalone Mongoose web server, and small self-contained executable, is a side effect, really. Therefore a strategic goal as I see it is integration with most languages such as C# (popular), Lua (often used by game devs), and so on.

However, I will take a look at SAPI. If it could be implemented more or less easily, I might consider doing it.

Comment by apriest...@messenger.org, May 17, 2009

why another layer when you have direct access to the web server? If we had a complete embedded example you would probably realize you don't even need SAPI, CGI if you can write code in C/C++ instead of PHP.

I'm using a C++ CGI library and have been running my exe with shttpd. If we had a complete embed example for mongoose I wouldn't need to do that and it would be even more "efficient". I think an embed example with the following would be helpful and make mongoose very popular:

1. secure login (which you have done) 2. serve a page (done) 3. submit a form and retrieve the variables (any length) 4. upload a file (of any length) and save to disk

Comment by valenok, May 17, 2009

Of course embedding Mongoose directly into C/C++ app gives most efficiency. Also I agree with your 4 steps.

However, coding in high level language such as PHP does have obvious benefits: much larger user community, ease of change, safety, and so on. To avoid CGI inefficiency, web server must handle requested page through the external processor in the serving thread instead of making a separate CGI process. External processor is a DLL with a processing API that is loaded by the web server. This is my simplistic understanding of SAPI, and the only major obstacle to implement it would be a cumbersome API, which I sort of expect from PHP. I could be wrong though, I haven't looked at it yet.

Comment by face00, Jun 15, 2009

I have created an objective-c class to make embedding mongoose in iPhone apps trivial. It can be found here: http://github.com/face/MongooseDaemon/tree/master

Comment by valenok, Jun 16, 2009

Would you mind putting the code into the Mongoose SVN?

Comment by face00, Jul 19, 2009

valenok - Yes that would be great, please let me know if I can help and or enhance it in any way.

Comment by valenok, Jul 19, 2009

Please organise the code in two files, bindings/objectivec/mongoose.m bindings/objectivec/example.c

and send a patch to me.

Comment by face00, Jul 21, 2009

Ok, let me think about it a bit. I just posted some bug fixes to my code as I'm making services now. However, what I have is more of of a convenience class than bindings.

The reason is because objective-c lets you call C directly without any bindings. I guess bindings would be ok for a purist type objective-c interface. However, I think it is fine just calling the C functions directly. It is like C++ in that way, no need bindings when you can just call the C methods.

Perhaps I should post a question to a Cocoa mailing list. I'm perfectly happy just calling the C methods directly from objective C, though I come from a C background and am not an objective-c purist....

Comment by face00, Jul 21, 2009

My class is much more like an example usage from objective-c than bindings....

Comment by j...@doatest.com, Jul 30, 2009

How about the process of the lua wrapper, I am interested at it, I am familiar with C/http/cgi/python and learn lua recently, if you share your plan/code of lua wrapper, I want see if I can do something for it?

Comment by beniukdima, Jul 31, 2009

Hi I can give you a code for lua, but it still needs some improvements. If you are interested please e-mail me on: beniukdima@gmail.com

Comment by Mhasaninejad, Sep 12, 2009

Hi,

I am new to mongoose. Can I run python cgi script with mongoose. Currently I am using c to write my CGI script for mongoose. I just want to know if python is native to mongoose or I need an interpreter. I have installed mongoose on a trafic controller which has only 4 MB Disk space.

Thanks, M.H

Thanks a lot


Sign in to add a comment
Hosted by Google Code