2013-09-09 07:35:57 +00:00
|
|
|
# protocol
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
The `protocol` module can register a new protocol or intercept an existing
|
2015-03-24 23:18:51 +00:00
|
|
|
protocol, so you can customize the response to the requests for various protocols.
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
An example of implementing a protocol that has the same effect with the
|
|
|
|
`file://` protocol:
|
|
|
|
|
|
|
|
```javascript
|
2015-06-09 16:10:02 +00:00
|
|
|
var app = require('app');
|
|
|
|
var path = require('path');
|
2014-05-18 10:33:10 +00:00
|
|
|
|
2015-03-24 11:56:12 +00:00
|
|
|
app.on('ready', function() {
|
2014-05-18 10:33:10 +00:00
|
|
|
var protocol = require('protocol');
|
|
|
|
protocol.registerProtocol('atom', function(request) {
|
|
|
|
var url = request.url.substr(7)
|
|
|
|
return new protocol.RequestFileJob(path.normalize(__dirname + '/' + url));
|
2015-07-07 14:43:13 +00:00
|
|
|
}, function (error, scheme) {
|
|
|
|
if (!error)
|
|
|
|
console.log(scheme, ' registered successfully')
|
2014-05-18 10:33:10 +00:00
|
|
|
});
|
2013-09-03 10:22:40 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2015-03-24 11:56:12 +00:00
|
|
|
**Note:** This module can only be used after the `ready` event
|
2013-09-20 10:36:16 +00:00
|
|
|
was emitted.
|
|
|
|
|
2015-07-07 14:43:13 +00:00
|
|
|
## protocol.registerProtocol(scheme, handler, callback)
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
* `scheme` String
|
|
|
|
* `handler` Function
|
2015-07-07 14:43:13 +00:00
|
|
|
* `callback` Function
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
Registers a custom protocol of `scheme`, the `handler` would be called with
|
2015-07-07 14:43:13 +00:00
|
|
|
`handler(request)` when the a request with registered `scheme` is made.
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
You need to return a request job in the `handler` to specify which type of
|
|
|
|
response you would like to send.
|
|
|
|
|
2015-07-16 13:32:09 +00:00
|
|
|
By default the scheme is treated like `http:`, which is parsed differently
|
|
|
|
from protocols that follows "generic URI syntax" like `file:`, so you probably
|
|
|
|
want to call `protocol.registerStandardSchemes` to make your scheme treated as
|
|
|
|
standard scheme.
|
|
|
|
|
2015-07-07 14:43:13 +00:00
|
|
|
## protocol.unregisterProtocol(scheme, callback)
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
* `scheme` String
|
2015-07-07 14:43:13 +00:00
|
|
|
* `callback` Function
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
Unregisters the custom protocol of `scheme`.
|
|
|
|
|
2015-06-12 07:58:23 +00:00
|
|
|
## protocol.registerStandardSchemes(value)
|
|
|
|
|
|
|
|
* `value` Array
|
|
|
|
|
2015-07-16 13:32:09 +00:00
|
|
|
`value` is an array of custom schemes to be registered as standard schemes.
|
|
|
|
|
|
|
|
A standard scheme adheres to what RFC 3986 calls
|
|
|
|
[generic URI syntax](https://tools.ietf.org/html/rfc3986#section-3). This
|
|
|
|
includes `file:` and `filesystem:`.
|
2015-06-12 07:58:23 +00:00
|
|
|
|
2015-07-05 17:53:07 +00:00
|
|
|
## protocol.isHandledProtocol(scheme, callback)
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
* `scheme` String
|
2015-07-05 17:53:07 +00:00
|
|
|
* `callback` Function
|
2013-09-03 10:22:40 +00:00
|
|
|
|
2015-07-05 17:53:07 +00:00
|
|
|
`callback` returns a boolean whether the `scheme` can be handled already.
|
2013-09-03 10:22:40 +00:00
|
|
|
|
2015-07-07 14:43:13 +00:00
|
|
|
## protocol.interceptProtocol(scheme, handler, callback)
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
* `scheme` String
|
|
|
|
* `handler` Function
|
2015-07-07 14:43:13 +00:00
|
|
|
* `callback` Function
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
Intercepts an existing protocol with `scheme`, returning `null` or `undefined`
|
|
|
|
in `handler` would use the original protocol handler to handle the request.
|
|
|
|
|
2015-07-07 14:43:13 +00:00
|
|
|
## protocol.uninterceptProtocol(scheme, callback)
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
* `scheme` String
|
2015-07-07 14:43:13 +00:00
|
|
|
* `callback` Function
|
2013-09-03 10:22:40 +00:00
|
|
|
|
|
|
|
Unintercepts a protocol.
|
|
|
|
|
|
|
|
## Class: protocol.RequestFileJob(path)
|
|
|
|
|
|
|
|
* `path` String
|
|
|
|
|
|
|
|
Create a request job which would query a file of `path` and set corresponding
|
|
|
|
mime types.
|
|
|
|
|
|
|
|
## Class: protocol.RequestStringJob(options)
|
|
|
|
|
|
|
|
* `options` Object
|
|
|
|
* `mimeType` String - Default is `text/plain`
|
|
|
|
* `charset` String - Default is `UTF-8`
|
|
|
|
* `data` String
|
|
|
|
|
|
|
|
Create a request job which sends a string as response.
|
2015-03-16 12:53:45 +00:00
|
|
|
|
|
|
|
## Class: protocol.RequestBufferJob(options)
|
|
|
|
|
|
|
|
* `options` Object
|
|
|
|
* `mimeType` String - Default is `application/octet-stream`
|
|
|
|
* `encoding` String - Default is `UTF-8`
|
|
|
|
* `data` Buffer
|
|
|
|
|
2015-05-07 07:42:35 +00:00
|
|
|
Create a request job which sends a buffer as response.
|
|
|
|
|
2015-06-17 03:32:39 +00:00
|
|
|
## Class: protocol.RequestHttpJob(options)
|
|
|
|
|
|
|
|
* `options` Object
|
2015-07-16 22:17:45 +00:00
|
|
|
* `session` [Session](browser-window.md#class-session) - By default it is
|
|
|
|
the app's default session, setting it to `null` will create a new session
|
|
|
|
for the requests
|
2015-06-17 03:32:39 +00:00
|
|
|
* `url` String
|
|
|
|
* `method` String - Default is `GET`
|
|
|
|
* `referrer` String
|
|
|
|
|
|
|
|
Send a request to `url` and pipe the response back.
|
|
|
|
|
2015-05-07 07:42:35 +00:00
|
|
|
## Class: protocol.RequestErrorJob(code)
|
|
|
|
|
|
|
|
* `code` Integer
|
|
|
|
|
|
|
|
Create a request job which sets appropriate network error message to console.
|
2015-06-17 03:32:39 +00:00
|
|
|
Default message is `net::ERR_NOT_IMPLEMENTED`. Code should be in the following
|
2015-05-11 04:54:44 +00:00
|
|
|
range.
|
2015-05-07 07:42:35 +00:00
|
|
|
|
|
|
|
* Ranges:
|
|
|
|
* 0- 99 System related errors
|
|
|
|
* 100-199 Connection related errors
|
|
|
|
* 200-299 Certificate errors
|
|
|
|
* 300-399 HTTP errors
|
|
|
|
* 400-499 Cache errors
|
|
|
|
* 500-599 ?
|
|
|
|
* 600-699 FTP errors
|
|
|
|
* 700-799 Certificate manager errors
|
|
|
|
* 800-899 DNS resolver errors
|
|
|
|
|
|
|
|
Check the [network error list](https://code.google.com/p/chromium/codesearch#chromium/src/net/base/net_error_list.h) for code and message relations.
|