MCPcopy Create free account
hub / github.com/angular/angular / handle

Method handle

packages/common/http/src/jsonp.ts:130–291  ·  view source on GitHub ↗

* Processes a JSONP request and returns an event stream of the results. * @param req The request object. * @returns An observable of the response events. *

(req: HttpRequest<never>)

Source from the content-addressed store, hash-verified

128 *
129 */
130 handle(req: HttpRequest<never>): Observable<HttpEvent<any>> {
131 // Firstly, check both the method and response type. If either doesn't match
132 // then the request was improperly routed here and cannot be handled.
133 if (req.method !== 'JSONP') {
134 throw new RuntimeError(
135 RuntimeErrorCode.JSONP_WRONG_METHOD,
136 ngDevMode && JSONP_ERR_WRONG_METHOD,
137 );
138 } else if (req.responseType !== 'json') {
139 throw new RuntimeError(
140 RuntimeErrorCode.JSONP_WRONG_RESPONSE_TYPE,
141 ngDevMode && JSONP_ERR_WRONG_RESPONSE_TYPE,
142 );
143 }
144
145 // Check the request headers. JSONP doesn't support headers and
146 // cannot set any that were supplied.
147 if (req.headers.keys().length > 0) {
148 throw new RuntimeError(
149 RuntimeErrorCode.JSONP_HEADERS_NOT_SUPPORTED,
150 ngDevMode && JSONP_ERR_HEADERS_NOT_SUPPORTED,
151 );
152 }
153
154 if (!this.isAllowedJsonpUrl(req.urlWithParams)) {
155 throw new RuntimeError(RuntimeErrorCode.JSONP_UNSAFE_URL, ngDevMode && JSONP_ERR_UNSAFE_URL);
156 }
157
158 // Everything else happens inside the Observable boundary.
159 return new Observable<HttpEvent<any>>((observer: Observer<HttpEvent<any>>) => {
160 // The first step to make a request is to generate the callback name, and replace the
161 // callback placeholder in the URL with the name. Care has to be taken here to ensure
162 // a trailing &, if matched, gets inserted back into the URL in the correct place.
163 const callback = this.nextCallback();
164 const url = req.urlWithParams.replace(/=JSONP_CALLBACK(&|$)/, `=${callback}$1`);
165
166 // Construct the <script> tag and point it at the URL.
167 const node = this.document.createElement('script');
168 node.src = url;
169
170 // Set the nonce for Content Security Policy compatibility. Without this,
171 // JSONP requests will be blocked by strict-dynamic CSP policies.
172 if (this.nonce) {
173 node.setAttribute('nonce', this.nonce);
174 }
175
176 // A JSONP request requires waiting for multiple callbacks. These variables
177 // are closed over and track state across those callbacks.
178
179 // The response object, if one has been received, or null otherwise.
180 let body: any | null = null;
181
182 // Whether the response callback has been called.
183 let finished: boolean = false;
184
185 // Set the response callback in this.callbackMap (which will be the window
186 // object in the browser. The script being loaded via the <script> tag will
187 // eventually call this callback.

Callers

nothing calls this directly

Calls 11

isAllowedJsonpUrlMethod · 0.95
nextCallbackMethod · 0.95
removeListenersMethod · 0.95
keysMethod · 0.65
createElementMethod · 0.65
setAttributeMethod · 0.65
addEventListenerMethod · 0.65
appendChildMethod · 0.65
cleanupFunction · 0.50
replaceMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected