Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: appengine_apps/trooper_o_matic/lib/net.html

Issue 774323002: Moved trooper_o_matic to appengine/ (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!--
2 Copyright (C) 2011 Google Inc. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 1. Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 THE POSSIBILITY OF SUCH DAMAGE.
24 -->
25
26 <script>
27 // TODO: Move sheriff-o-matic to this repository and merge the net
28 // library.
29
30 var net = net || {};
31
32 (function () {
33 var cache = {};
34 var cacheURLs = [];
35 var cacheLimit = 1000;
36
37 function updateCache(url, text)
38 {
39 var alreadyExists = url in cache;
40 cache[url] = text;
41 if (alreadyExists)
42 return;
43 cacheURLs.push(url);
44 if (cacheURLs.length > cacheLimit)
45 delete cache[cacheURLs.shift()];
46 }
47
48 function clearCache(url)
49 {
50 cacheURLs.remove(cacheURLs.indexOf(url))
51 delete cache[url];
52 }
53
54 net.json = function(options)
55 {
56 return net.ajax(options).then(JSON.parse);
57 };
58
59 net.ajax = function(options)
60 {
61 return new Promise(function(resolve, reject) {
62 if (options.url in cache) {
63 if (options.cache) {
64 resolve(cache[options.url]);
65 return;
66 } else {
67 clearCache(options.url);
68 }
69 }
70 var xhr = new XMLHttpRequest();
71 var method = options.type || 'GET';
72 var async = true;
73 xhr.open(method, options.url, async);
74 xhr.onload = function() {
75 if (xhr.status == 200) {
76 if (options.cache)
77 updateCache(options.url, xhr.responseText);
78 resolve(xhr.responseText);
79 } else if (xhr.status != 200) {
80 reject(Error(xhr.statusText));
81 }
82 };
83 xhr.onerror = function(error) {
84 reject(error);
85 };
86 var data = options.data || null;
87 if (data)
88 xhr.setRequestHeader("content-type","application/x-www-form-urlencod ed");
89 xhr.send(data);
90 });
91 };
92
93 net.probe = function(url)
94 {
95 return net.ajax({
96 url: url,
97 type: 'HEAD',
98 });
99 };
100
101 net._parseJSONP = function(jsonp)
102 {
103 if (!jsonp)
104 return {};
105
106 if (!jsonp.match(/^[^{[]*\(/))
107 return JSON.parse(jsonp);
108
109 var startIndex = jsonp.indexOf('(') + 1;
110 var endIndex = jsonp.lastIndexOf(')');
111 if (startIndex == 0 || endIndex == -1)
112 return {};
113 return JSON.parse(jsonp.substr(startIndex, endIndex - startIndex));
114 };
115
116 // We use XMLHttpRequest and CORS to fetch JSONP rather than using script tags.
117 // That's better for security and performance, but we need the server to coopera te
118 // by setting CORS headers.
119 net.jsonp = function(options)
120 {
121 return net.ajax(options).then(function(jsonp) {
122 return net._parseJSONP(jsonp);
123 }).catch(function(error) {
124 return {};
125 });
126 };
127
128 })();
129
130 </script>
OLDNEW
« no previous file with comments | « appengine_apps/trooper_o_matic/lib/log.html ('k') | appengine_apps/trooper_o_matic/lib/network-simulator.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698