Class opera.io.webserver
Object | +--opera.io.webserver
- class opera.io.webserver
Web server implementation
The WebServer offers access to information about the proxy, device and currently running service. It also maintains incoming connections.
Services running on the Web server are accessed through a URL on the form:
http://device.host.proxy/service/path, e.g.
http://work.john.operaunite.com/wiki/addEntry
Defined in unite.js
| Field Summary | |
Object |
connections
The current connections made to this Web server. |
String |
currentServiceName
The name of the service currently accessing the webserver object, for example 'File Sharing'. |
String |
currentServicePath
The path of the service currently accessing the webserver object, for example '/fileSharing/'. |
String |
deviceName
The name of the device the Web server is running on, for example 'work'. |
String |
hostName
The hostname of the Webserver, for example 'work.john.operaunite.com'. |
string |
originURL
The URL the currently running service was downloaded from. |
String |
port
The port this Web server is listening to, for example 8840. |
String |
proxyName
The name of the proxy the Web server is connected to, for example 'operaunite.com'. |
String |
publicIP
The public facing IP address of this Web server, as seen by the proxy. |
int |
publicPort
The public facing port of this Web server, as seen by the proxy. |
Object |
services
Services running on this Web server. |
String |
userName
The My Opera user name of the user owning the Web server, for example 'john'. |
| Constructor Summary | |
opera.io.webserver
()
This class has no constructor. |
|
| Method Summary | |
void
|
addEventListener( <String> pathFragment, <Function> handler, <boolean> useCapture )
Add an event listener for incoming requests. |
String
|
getContentType( <String> file )
Get the MIME content type mapped to a particular file name. |
void
|
removeEventListener( <String> pathFragment, <Function> handler, <boolean> useCapture )
Remove an event listener from the server. |
void
|
shareFile( <File> file, <String> path )
Shares a File |
void
|
sharePath( <String> path, <File> file )
Share the given file on the given URL path. |
void
|
unshareFile( <File> file )
Unshares a previously shared file |
void
|
unsharePath( <String> path )
Unshare a File previously shared on the given URL path |
| Field Detail |
connections
Object connections
Array-like object containing WebServerConnection objects representing the current connections to this Web server.
Connections remain in this collection even if they are closed. They are removed when there are no longer any references to the connection elsewhere in the system.
currentServiceName
String currentServiceName
The name of the service, for example as defined in the widgetname element
in the config.xml of a Opera Unite application.
currentServicePath
String currentServicePath
The path of the service, for example as defined in the servicePath element
in the config.xml of an Opera Unite application. In contrast to the serviceName,
this name can only contain characters that are valid in an IRI.
The path includes a leading and trailing slash.
This way a service called "My Cool File Sharing (tm) (c)" can be
identified as simply "share" in it's URL, i.e.
http://work.john.operaunite.com/share.
In the example above this property would contain '/share/'.
Note that anything after the first path component is handled by the service.
deviceName
String deviceName
work as in http://work.john.operaunite.com.
hostName
String hostName
You may run Web servers on different devices. The hostname contains the device name,
username and proxy address, for example work.john.operaunite.com
as in http://work.john.operaunite.com/wiki.
Note that this will always be a host name which contains the proxy name.
This property will not contain the 'admin.' subdomain, if it is used.
originURL
string originURL
This property can be used to make a download link to the service and can also function as part of an auto update scheme.
port
String port
proxyName
String proxyName
operaunite.com as in http://work.john.operaunite.com/wiki
publicIP
String publicIP
null.
publicPort
int publicPort
null.
services
Object services
userName
String userName
| Constructor Detail |
opera.io.webserver
opera.io.webserver()
| Method Detail |
addEventListener
void addEventListener( <String> pathFragment, <Function> handler, <boolean> useCapture )
Listening for requests is done by registering event listeners on the Web server. The "event name" corresponds to the path fragment of the URL after the service name, e.g. 'add' as in http://work.john.operaunite.com/wiki/add.
Registered event handlers are called with an event of the type WebServerRequestEvent.
Any request names starting with underscore ('_') is reserved and cannot be used.
The exceptions are the following request names, which have special meanings:
- _index
- Event fired when a user accesses the root of the service (i.e. http://work.john.operaunite.com/wiki). Use this to supply a default start page or similar for your service.
- _request
- Event fired when a user accesses any URL under the service. Use this to catch
all requests to the server in a general fashion. You'll need to use the
WebServerRequest.uri to distinguish the actual request URI. Listening for this event will also catch
the
_indexrequest, but not_close. - _close
- Event fired when a connection is closed. In this case the
connectionproperty of theeventobject is null.
Events for specific event listeners and _index events are fired before the _request event is fired. Consider the following code example:
opera.io.webserver.addEventListener('_request', generalhandler, false);
opera.io.webserver.addEventListener('add', addhandler, false);
The handlers for a specific path, including _index is called before _request.
If the user visits the URL http://work.john.operaunite.com/wiki/add, a WebServerRequestEvent is fired,
and the addhandler is called, before the generalhandler
method is called. This happens regardless of which event listener was registered first.
pathFragment - Path fragment to add a listener for. handler - Event listener function to add. useCapture - Whether or not the capture phase should be used. getContentType
String getContentType( <String> file )
file - The file name to get a MIME type for, for example 'index.html'. removeEventListener
void removeEventListener( <String> pathFragment, <Function> handler, <boolean> useCapture )
pathFragment - Path fragment to remove a listener for. handler - Event listener function to remove. useCapture - Whether or not this applies to the capture phase. shareFile
void shareFile( <File> file, <String> path )
Shares a File from a mountpoint that has been acquired earlier, and makes it available under the path specified in the second argument.
The File can be a regular file, a directory or an archive.
The share is automatically deleted when the application is closed.
Example: If you have resolved a File to a given folder and then specify opera.io.webserver.shareFile(myFile, 'share'),
it will be shared as the URL http://device.user.proxy/service/share
file - The File to share path - The path this file will be shared as on the Web. sharePath
void sharePath( <String> path, <File> file )
The path must be a valid URL path token, and should be a relative sub path of the service. It should not start with '../' or '/', but you may specify additional path tokens, like 'media/gfx/logo.png'.
The File can be a regular file, a directory or an archive, but it must be referenced by a mount point.
ALREADY_SHARED_ERR will be thrown if something is already shared on the given path The share is automatically deleted when the application is closed.
Note that if you have added a _request event handler, you will need to compare the incoming requests
with the paths you have shared to see if the request matches a shared file. If so, you will need to call
WebServerResponse.closeAndRedispatch() without changing the uri fo the request to send the request back to
the Web server. On the second pass, the Web server will serve the shared file directly.
Example: In order to share a resolved File on the path 'media/gfx', you would do the following:
opera.io.webserver.sharePath('media/gfx', myFile);
The file will now be shared as the URL http://device.user.proxy/service/media/gfx.
path - URL path to share something on. file - File to share on the given path. unshareFile
void unshareFile( <File> file )
file - The file to unshare. unsharePath
void unsharePath( <String> path )
When a file has been shared using sharePath(), it can be unshared again by calling this method with the same path used to share the file.
Example: Assume that a file has been shared on the path 'media/gfx', i.e. on the URL
http://device.user.proxy/service/media/gfx/, calling this method as follows will unshare the file:
opera.io.webserver.unsharePath('media/gfx');path - URL path of the file to unshare.