Simple web server

straybullet

Member
Licensed User
Longtime User
Is there a simple web server for B4A ie library I am missing that will allow a B4A app to connect to it's self locally to by-pass the stupid built in security to run JavaScript and html5 in our webviews?

How does phone gap get around this, wonder if it has a built in web server, seems to me we could really open up some awesome possibilities with html5 in our apps.
 

warwound

Expert
Licensed User
Longtime User
I think that what we're looking for is a WebServer object that runs on the device.

Imagine an Apache webserver running on the device - any application can request a file from http://localhost/.

Maybe the webserver could support PHP but that's not relevant for now (and would require a very bloated library).

A simple WebServer that has a root folder property and once running will serve HTTP requests from that folder would be ideal.

What we need is an alternative to the file system and the file protocol (file:///) for web apps that use features that are not supported when a webpage is loaded using the file protocol.

Another use that i would make use of is where i have device generated KML file and want to display that KML using the device's native Map application.

The native Map application can be started with an Intent specifying a URL to a publically accessible KML file.
If you launch an Intent that tries to load a KML file from the file system then the Map application will fail to load that KML - Google servers need to be able to load that KML file via the internet so that it can generate the layer to display on the map.
An Intent with a file protocol KML file will never be rendered as the Google servers cannot retrieve the KML from the device's file system and therefore cannot convert the KML file into an overlay layer to be displayed in the Map appilication.

I looked at this just a few weeks back and found no simple solution.
One or two old and no longer maintained non-Android libraries with lots of problems/bugs but nothing that could be easily converted into a B4A library.

Even if Erel and the team could create a new WebServer object there could still be problems if that WebServer was required to be accessible from the internet and not just from the local device.
Many devices connect to the (3G) internet using Network Address Translation (NAT) and share a common IP address.
Such a device is not going to be accessible with that IP address - yet more code would be required to make the device and the WebServer running on it truly online.

Martin.
 
Upvote 0

straybullet

Member
Licensed User
Longtime User
Martin nailed it.

It would be great if it ran from within the app like an extended library, you set the port it listens to and then local webview could then connect to it and run HTML5 level code which opens up a whole new realm of usefulness.

Currently Webview restricts a lot because the content I load locally is not allowed to run much over basic JavaScript at best.

To me the folder the web server uses is the same one the app is running from ie assets folder.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I can make a lighteight server object using the regular server object.
it would only be for the html, the images would still be in your asset folder
 
Upvote 0

straybullet

Member
Licensed User
Longtime User
Sounds like a step in the right direction, but how do we reference the images inside the html doc

Right now everything I put is all in one folder and do not use paths to get the images to display.

So if I used the http server to display say index.html and inside that my images are referenced with no path would it still work?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Even if Erel and the team could create a new WebServer object there could still be problems if that WebServer was required to be accessible from the internet and not just from the local device.
Many devices connect to the (3G) internet using Network Address Translation (NAT) and share a common IP address.
Such a device is not going to be accessible with that IP address - yet more code would be required to make the device and the WebServer running on it truly online.
Martin.
How do we make it accessible over 3G/NAT? A LOT of 3G operators block incoming connections, as does NAT. Is this possible without another visible server (punchthrough)?
 
Upvote 0

straybullet

Member
Licensed User
Longtime User
The web server is not to let your phone become a server to the world, but to by-pass the restrictions of running content locally so you can do so much more with your web view full html 5 for example.


How do we make it accessible over 3G/NAT? A LOT of 3G operators block incoming connections, as does NAT. Is this possible without another visible server (punchthrough)?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I think we have two ideas going on in this thread...

straybullet wants to use a webserver on the device and load content from that server into an HTML5 webpage.
That will bypass security restrictions that apply if the same content was loaded using the file protocol.

What i was interested in was running a webserver on the device so that the device could host some KML files.
I want to use the native Android Maps application to display these KML files.
In order for the Maps app to display these KML files, the KML files must be accessible by the Google webservers via the internet.

So straybullet requires a webserver that is accessible only to the device that it is running on, whereas i'd require the webserver to be accessible via the internet.

@straybullet
Can you give some details of which HTML5 features you have found that will not work using content loaded with the file protocol?

Martin.
 
Upvote 0

straybullet

Member
Licensed User
Longtime User
I think it would be easier to post the ones that do work, but I am using an HTML5 development environment that build the code, I have tried Javascript commands and many of them that use dhtml break as well.


I think if a server was built it could be contacted from the outside as well as locally, the issue you will have to deal with is NAT if the provider does not give out real IPs. But regardless it will work the same as local, just 127.0.0.1 will always work, unlike the IP that might be shared with your provider's customers.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I wrote a simple web server.

However upon testing it with a webview, it would not execute javascript.
 

Attachments

  • HTTP.zip
    321.9 KB · Views: 1,331
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

When you say javascript doesn't work are you basing that on the fact that no alert dialogs show with your demo code?

The default B4A WebView does not display any of the javascript modal dialogs.
Check out my WebViewExtras.

The default B4A WebView has no WebChromeClient.
No console messages in your javascript will ever appear in your application log.
And no javascript modals - alert(), prompt() and confirm() - will ever be displayed.

The WebViewExtras WebChromeClient passes javascript console messages to your application log and displays alert(), prompt() and confirm() modal dialogs.

I added WebViewExtras to your demo and added a WebChromeClient to the WebView.
Your alert dialog now displays.

I then created a simple webpage to test javascript support and it works perfectly, this is the webpage HTML:

B4X:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript test</title>
<script type="text/javascript">
function test(){
   var myDiv=document.getElementById('myDiv');
   myDiv.style.backgroundColor='#EE0000';
   myDiv.style.color='#000000';
   myDiv.innerHTML='Has the javascript test worked?';
}
</script>
</head>

<body>
<div id="myDiv">
Original div content
<a href="javascript:void(0)" onclick="test()">Click here</a>
</div>
</body>
</html>

I've attached my updated version of your demo to this post.

Anyway a big congrats for the webserver code - i don't have time to look in detail at it right now but will do so later. :sign0188:

Martin.
 

Attachments

  • WebServer.zip
    8.7 KB · Views: 1,234
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
--Explanation--

Ah. My bad.

Anyway a big congrats for the webserver code

Thank you.

I went the non-lazy route with it. It supports virtual folders like a real server would (customizable, of course) and it parses the entire HTTP header.
I meant to add an option to only allow localhost, but I couldn't figure out how to get the connected IP address
It also stops listening on activity_pause, it's supposed to re-start on activity_resume but didn't. But I'm sure you guys can figure it out.
 
Last edited:
Upvote 0

NilsBe

Member
Licensed User
Longtime User
dont use CRLF because: CRLF <> chr(13)&chr(10)

I wrote a simple web server.

I liked your example of a simple webserver.

I have spent some time playing with it and also adding the function of sending images. It took me quite some time to find the reason for a strange behaviour when requesting files with chrome on a pc from the webserver on my android phone.

The problem was CRLF. On Android CRLF = chr(10). This violates the definition of the HTTP protocol. In order to avoid problems we cannot use CRLF and have to use chr(13)&chr(10) instead.

Nils
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi Martin,

I would like to host a website on a tablet which is connected to my local network. I also have a NoIP account so I can redirect any outcomming request to the tablet.

I've try your example but I always get "Access Error: 404 -- Not Found" message. Do you have a new version of the example or explain me how tu correctly use it ?

Thank you very much for your time.

Sorry my post should have been post here : http://www.b4x.com/forum/basic4android-updates-questions/13683-simple-web-server-2.html

I don't really know much about the simple webserver - it's not something i've tried to use myself.

When you get the 404 Not Found error, is that error generated by the simple webserver?

Are you trying to connect to the simple webserver from the device running the webserver or from another device?
Does it work locally but a network problem is preventing network access?

Are you sure that the file that you are requesting exists?

Martin.

(This replies to the post here: http://www.b4x.com/forum/additional...updates/12453-webviewextras-4.html#post130547)
 
Upvote 0

Toley

Active Member
Licensed User
Longtime User
Thank you for your answer Martin, I've done a little more test and the example from NeoTechni seems to work like I want.
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
Hello Guys,

Interesting stuff here.

I have not tried yet, but want to know it this simple server can server real pages, over wifi connection.

I want to use the tablet to be my own little server for my support application, very few connections, lets say, 1 per hour :) I think the tablet can handle it...

However I wanted it to server better possible in pages.

Can I do that using this simple server? What is the limitation, any of you guys made any improvement?

Thanks

Eduardo
 
Upvote 0
Top