blob: d04a1d55bcfe5d41235442ab79d2445d7143807d [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: odata/net.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: odata/net.js</h1>
<section>
<article>
<pre class="prettyprint source"><code>/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
'use strict';
/** @module odata/net */
var http = require('http');
var utils = require('./../utils.js');
var url = require("url");
var defined = utils.defined;
var delay = utils.delay;
var ticks = 0;
/* Checks whether the specified request can be satisfied with a JSONP request.
* @param request - Request object to check.
* @returns {Boolean} true if the request can be satisfied; false otherwise.
* Requests that 'degrade' without changing their meaning by going through JSONP
* are considered usable.
*
* We allow data to come in a different format, as the servers SHOULD honor the Accept
* request but may in practice return content with a different MIME type.
*/
function canUseJSONP(request) {
return false;
}
/** Checks whether the specified URL is an absolute URL.
* @param {String} url - URL to check.
* @returns {Boolean} true if the url is an absolute URL; false otherwise.
*/
function isAbsoluteUrl(url) {
return url.indexOf("http://") === 0 ||
url.indexOf("https://") === 0 ||
url.indexOf("file://") === 0;
}
/** Checks whether the specified URL is local to the current context.
* @param {String} url - URL to check.
* @returns {Boolean} true if the url is a local URL; false otherwise.
*/
function isLocalUrl(url) {
if (!isAbsoluteUrl(url)) {
return true;
}
// URL-embedded username and password will not be recognized as same-origin URLs.
var location = window.location;
var locationDomain = location.protocol + "//" + location.host + "/";
return (url.indexOf(locationDomain) === 0);
}
/** Reads response headers into array.
* @param {Object} inHeader
* @param {Array} outHeader
*/
function readResponseHeaders(inHeader, outHeader) {
for (var property in inHeader) {
if (inHeader.hasOwnProperty(property)) {
outHeader[property] = inHeader[property];
}
}
}
exports.defaultHttpClient = {
formatQueryString: "$format=json",
/** Performs a network request.
* @param {Object} request - Request description
* @param {Function} success - Success callback with the response object.
* @param {Function} error - Error callback with an error object.
* @returns {Object} Object with an 'abort' method for the operation.
*/
request: function (request, success, error) {
var result = {};
var done = false;
var options = url.parse(request.requestUri);
options.method = request.method || "GET";
options.headers = {};
//options.auth = request.user + ':' + request.password;
//add headers
var name;
if (request.headers) {
for (name in request.headers) {
options.headers[name] = request.headers[name];
}
}
var xhr = http.request(options);
result.abort = function () {
if (done) {
return;
}
done = true;
if (xhr) {
xhr.abort();
xhr = null;
}
error({ message: "Request aborted" });
};
// Set the timeout if available.
if (request.timeoutMS) {
xhr.setTimeout(request.timeoutMS,function () {
if (!done) {
done = true;
xhr = null;
error({ message: "Request timed out" });
}
});
}
xhr.on('error', function(e) {
var response = { requestUri: url, statusCode: 400, statusText: e.message };
error({ message: "HTTP request failed", request: request, response: response });
});
xhr.on('response', function (resp) {
if (done || xhr === null) {
return;
}
var headers = [];
readResponseHeaders(resp.headers, headers);
var body = '';
resp.on('data', function(chunk) {
body+=chunk;
});
resp.on('end', function() {
// do what you do
var response = { requestUri: url, statusCode: resp.statusCode, statusText: '', headers: headers, body: body };
done = true;
xhr = null;
if (resp.statusCode >= 200 && resp.statusCode &lt;= 299) {
success(response);
} else {
error({ message: "HTTP request failed", request: request, response: response });
}
});
});
//xhr.open(request.method || "GET", url, true,);
if (request.body) {
xhr.write(request.body);
}
xhr.end();
return result;
}
};
exports.canUseJSONP = canUseJSONP;
exports.isAbsoluteUrl = isAbsoluteUrl;
exports.isLocalUrl = isLocalUrl;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Index</a></h2><h3>Modules</h3><ul><li><a href="module-cache.html">cache</a></li><li><a href="source.html">cache/source</a></li><li><a href="module-odata.html">odata</a></li><li><a href="batch.html">odata/batch</a></li><li><a href="handler.html">odata/handler</a></li><li><a href="json.html">odata/json</a></li><li><a href="metadata.html">odata/metadata</a></li><li><a href="net.html">odata/net</a></li><li><a href="utils.html">odata/utils</a></li><li><a href="deferred.html">odatajs/deferred</a></li><li><a href="utils_.html">odatajs/utils</a></li><li><a href="xml.html">odatajs/xml</a></li><li><a href="module-store.html">store</a></li><li><a href="dom.html">store/dom</a></li><li><a href="indexeddb.html">store/indexeddb</a></li><li><a href="memory.html">store/memory</a></li></ul><h3>Classes</h3><ul><li><a href="DataCache.html">DataCache</a></li><li><a href="DataCacheOperation.html">DataCacheOperation</a></li><li><a href="DjsDeferred.html">DjsDeferred</a></li><li><a href="dom-DomStore.html">DomStore</a></li><li><a href="indexeddb-IndexedDBStore.html">IndexedDBStore</a></li><li><a href="memory-MemoryStore.html">MemoryStore</a></li><li><a href="ODataCacheSource.html">ODataCacheSource</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br clear="both">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Thu Apr 09 2015 08:31:26 GMT+0200 (MESZ)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>