WEBSERVER EXTENSION FOR FEZ COBRA using SD Card help

I have one question…
I get exception:


    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (3) ####
    #### Message: 
    #### ChipworkXWEBServer.Program::bsc_lanForm [IP: 0008] ####
    #### ChipworkXWEBServer.WebServer::ThreadProc [IP: 0283] ####
A first chance exception of type 'System.NullReferenceException' occurred in ChipworkX Application.exe
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (7) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::poll [IP: 0000] ####
    #### System.Net.Sockets.Socket::Poll [IP: 0011] ####
    #### System.Net.Sockets.Socket::Accept [IP: 0017] ####
    #### System.Net.HttpListener::AcceptThreadFunc [IP: 0020] ####
    #### SocketException ErrorCode = -1728053248
    #### SocketException ErrorCode = -1728053248
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = -1728053248
    #### SocketException ErrorCode = -1728053248
The thread '<No Name>' (0x7) has exited with code 0 (0x0).
Exception was thrown: System.NullReferenceException
8
9
    #### Exception System.Net.ProtocolViolationException - 0x00000000 (3) ####
    #### Message: Invalid HTTP request String: 
    #### System.Net.HttpListenerRequest::ParseHTTPRequest [IP: 003e] ####
    #### System.Net.HttpListenerContext::get_Request [IP: 000d] ####
    #### ChipworkXWEBServer.WebServer::ThreadProc [IP: 003e] ####
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Http.dll
Socket already closed
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (8) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::poll [IP: 0000] ####
    #### System.Net.Sockets.Socket::Poll [IP: 0011] ####
    #### System.Net.Sockets.Socket::Accept [IP: 0017] ####
    #### System.Net.HttpListener::AcceptThreadFunc [IP: 0020] ####
    #### SocketException ErrorCode = -1728053248
    #### SocketException ErrorCode = -1728053248
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = -1728053248
    #### SocketException ErrorCode = -1728053248
The thread '<No Name>' (0x8) has exited with code 0 (0x0).
The program '[13] Micro Framework application: Managed' has exited with code 0 (0x0).

problem is when I directly access to web page which need some POST data. For example if I send from some other site POST data all is ok. If for example I wan’t check if there is some POST data then I get exception…
Code:


 if (e.Post.Count > 0)
            {
                Debug.Print("username:" + e.Post.Contains("username").ToString());
                Debug.Print("session:" + e.Post.Contains("session").ToString());
            }

Any sugestion?

@ Dejan

The hashtables e.Post and e.Get are only created by the webserver class if there is something to put into.

So when there is no post data, e.Post will be null.

You might use it like this:


if (e.Post != null)
{
    Debug.Print("username:" + e.Post.Contains("username").ToString());
    Debug.Print("session:" + e.Post.Contains("session").ToString());
}

Loooooool Thanks Wouter Huysentruit I don’t know how I didn’t try this simple comparation instead of looking for count or some other method :smiley:
Now work ok :slight_smile:
This WebServer extension is great thing I can combine images from SD card together with html from Resources…
One question:
What all I can use in my HTML code?
Can I use:
HTML
CSS
PHP
JavaScript(jQuery, AJAX…)

Or are here some limits what we can use for making pages?

PHP will not go, unless you write your own PHP parser.

But I use it in this combo:
XHTML, CSS, Javascript(prototype library & scriptacoulus) with AJAX that requests JSON objects

So you can do a lot with it :slight_smile:

Basically anything that is server side you have to implement. Client side technologies should work as is.

So if I understand right for example I make HTML file put it on desktop and open it from firefox. If work normaly without installed any HTTP server then it will also work on cobra?

that’s right

I have one question.
Is posible from POST data get file from client?
For example I create webpage which have input:


<input name="configuration" id="configuration" size="20" type="file">

How I can get this file to Cobra?
Thanks for any help.

That is not yet implemented. I’ll see if I can implement it, stay tuned :slight_smile:

Wouter Huysentruit Thanks.
Strings in POST work ok :slight_smile: What I would like is that I from POST get file and save it to SDcard.
For example:
User can now when we have BinaryResponse save configuration file from device to HDD(Like router’s configuration) and then I would like have option to save it back to device(Like router’s load configuration from HDD option)…

I know what you mean.

You’ll need to add this to your form-tag

enctype="multipart/form-data"

Then the webserver will receive form-data encoded block which I need to process in a different way as I do now.

To keep the current concept, I’ll have to buffer the data in memory, which means that the size of the uploaded data will be limited to, let’s say, 256Kbyte. Is that a problem?

I already using enctype= “multipart/form-data” so I will leave it as it is :slight_smile:
My current config file have size 1,33 KB (1.368 B) and it will not increase a lot. Maybe few KB but it will be under limitation. Is maybe possible make this buffer configurable(for example in WEBServer.cs some constant like current POST comannds)?
Thanks for your time and help.

Hi. Again me :slight_smile:
I have another question:
1.)Is possible add option for permited IP addresses whcih have access to webpage? For example:
If we use webserver.permitedIP = “0.0.0.0” then all IP addresses have access to web page.
If we use webserver.permitedIP = “192.168.1.11” then only 192.168.1.11 IP address have access to web page.
If we use webserver.permitedIP = “192.168.1.11;192.168.1.12;192.168.1.13” then only 192.168.1.11, 192.168.1.12 and 192.168.1.13 IP address have access to web page.
If we use webserver.permitedIP = “192.168.1.0” then all 192.168.1.XXX IP address have access to web page.

If user who doesn’t have permited IP access connect to webpage receive default error page(maybe new option like:
webserver.ErrorPage(new WebServer.EventCallback(error_page));

What do you think?

@ Dejan: I can only handle one request at a time :stuck_out_tongue:

I’m a little stuck with that multipart thing.
The post data of a multipart block is seperated by a textual boundary.

I have a loop that searches the index of all boundaries inside the received data. But man, it is slow! It only processes 700bytes per second! So if you send a file of 15K it takes more then 20 seconds to find the boundaries…


                // Scanning input for boundaries
                Queue boundaryOffsets = new Queue();
                int matchLength = 0;
                for (int i = 0; i < dataLength; i++)
                {
                    if (data[i] == (byte)boundary[matchLength++])
                    {
                        if (matchLength == boundary.Length)
                        {
                            // Found match
                            boundaryOffsets.Enqueue(i - (matchLength - 1));
                            matchLength = 0;
                        }
                    }
                    else
                    {
                        if (matchLength > 0)
                            i -= (matchLength - 1);

                        matchLength = 0;
                    }
                }

Does anyone know of a faster way of doing this?
I can use RLP for that, but then the WebServer class is not that plug&play anymore.

This small change brought it to 945bytes / second, which is still too slow.


                // Scanning input for boundaries
                Queue boundaryOffsets = new Queue();
                int matchLength = 0;
                for (int i = 0; i < dataLength; i++)
                {
                    if (data[i] == (byte)boundary[matchLength])
                    {
                        if (++matchLength == boundary.Length)
                        {
                            // Found match
                            boundaryOffsets.Enqueue(i - (matchLength - 1));
                            matchLength = 0;
                        }
                    }
                    else
                    {
                        if (matchLength > 0)
                        {
                            i -= (matchLength - 1);
                            matchLength = 0;
                        }
                    }
                }

@ Wouter Huysentruit: If there is no way to speed-up thing then is better to have slow one than nothing :slight_smile: We will not upload firmware files by web browser so 1kb/s will be good for smal configuration files(few kb)…

Wouter,

Sorry if this is a stupid question
Is it possible to use html files from an SD card (by using the virtual directory option) and insert active content into the static files ?

Stefaan

That is possible if you modify the WebServer class :slight_smile:

Anyway, I would suggest you host a static website with client-side scripting (use a javascript framework like jquery or prototype). This way you can have the client-size scripting requesting values from the webserver that you handle with a callback.