fix response headers modification
authorRobo <hop2deep@gmail.com>
Fri, 4 Dec 2015 01:54:01 +0000 (07:24 +0530)
committerRobo <hop2deep@gmail.com>
Wed, 9 Dec 2015 14:44:18 +0000 (20:14 +0530)
atom/browser/net/atom_network_delegate.cc
docs/api/session.md

index 1ee886b..d4c8399 100644 (file)
@@ -44,9 +44,10 @@ bool MatchesFilterCondition(
     for (auto& pattern : info.url_patterns)
       if (pattern.MatchesURL(url))
         return true;
+    return false;
   }
 
-  return false;
+  return true;
 }
 
 base::DictionaryValue* ExtractRequestInfo(net::URLRequest* request) {
@@ -106,10 +107,20 @@ void OnBeforeSendHeadersResponse(
 
 void OnHeadersReceivedResponse(
     const net::CompletionCallback& callback,
+    const net::HttpResponseHeaders* original_response_headers,
     scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
     const AtomNetworkDelegate::BlockingResponse& result) {
-  if (result.responseHeaders.get())
-    *override_response_headers = result.responseHeaders;
+  if (result.responseHeaders.get()) {
+    *override_response_headers = new net::HttpResponseHeaders(
+        original_response_headers->raw_headers());
+    void* iter = nullptr;
+    std::string key;
+    std::string value;
+    while (result.responseHeaders->EnumerateHeaderLines(&iter, &key, &value)) {
+      (*override_response_headers)->RemoveHeader(key);
+      (*override_response_headers)->AddHeader(key + ": " + value);
+    }
+  }
   callback.Run(result.Cancel());
 }
 
@@ -242,6 +253,7 @@ int AtomNetworkDelegate::OnHeadersReceived(
         base::Bind(wrapped_callback, details),
         base::Bind(&OnHeadersReceivedResponse,
                    callback,
+                   original_response_headers,
                    override_response_headers));
 
     return net::ERR_IO_PENDING;
@@ -290,12 +302,14 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
     details->Set("responseHeaders",
                  GetResponseHeadersDict(request->response_headers()));
     details->SetBoolean("fromCache", request->was_cached());
+
+    auto response_headers = request->response_headers();
     details->SetInteger("statusCode",
-                        request->response_headers() ?
-                            request->response_headers()->response_code() : 200);
+                        response_headers ?
+                            response_headers->response_code() : 200);
     details->SetString("statusLine",
-                       request->response_headers() ?
-                          request->response_headers()->GetStatusLine() : std::string());
+                       response_headers ?
+                          response_headers->GetStatusLine() : std::string());
 
     BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                             base::Bind(base::IgnoreResult(wrapped_callback),
@@ -327,12 +341,14 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
     details->Set("responseHeaders",
                  GetResponseHeadersDict(request->response_headers()));
     details->SetBoolean("fromCache", request->was_cached());
+
+    auto response_headers = request->response_headers();
     details->SetInteger("statusCode",
-                        request->response_headers() ?
-                            request->response_headers()->response_code() : 200);
+                        response_headers ?
+                            response_headers->response_code() : 200);
     details->SetString("statusLine",
-                       request->response_headers() ?
-                          request->response_headers()->GetStatusLine() : std::string());
+                       response_headers ?
+                          response_headers->GetStatusLine() : std::string());
 
     BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                             base::Bind(base::IgnoreResult(wrapped_callback),
index d889ace..b16a9b1 100644 (file)
@@ -309,7 +309,7 @@ myWindow.webContents.session.webRequest.onBeforeSendHeaders(filter, function(det
 
 * `filter` Object
   * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
-                          will be filtered out.
+                   will be filtered out.
 * `listener` Function
   * `details` Object
     * `id` String - Request id.
@@ -320,7 +320,7 @@ myWindow.webContents.session.webRequest.onBeforeSendHeaders(filter, function(det
 * `blockingResponse` Object
   * `cancel` Boolean - Whether to continue or block the request.
   * `redirectURL` String **optional** - The original request is prevented from being sent or
-                                                         completed, and is instead redirected to the given URL.
+                                        completed, and is instead redirected to the given URL.
 
 Fired when a request is about to occur. Should return a `blockingResponse`.
 
@@ -328,7 +328,7 @@ Fired when a request is about to occur. Should return a `blockingResponse`.
 
 * `filter` Object
   * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
-                          will be filtered out.
+                   will be filtered out.
 * `listener` Function
   * `details` Object
     * `id` String - Request id.
@@ -340,7 +340,7 @@ Fired when a request is about to occur. Should return a `blockingResponse`.
 * `blockingResponse` Object
   * `cancel` Boolean - Whether to continue or block the request.
   * `requestHeaders` Object **optional** - When provided, request will be made with these
-                                                                    headers.
+                                           headers.
 
 Fired before sending an HTTP request, once the request headers are available. This may
 occur after a TCP connection is made to the server, but before any http data is sent.
@@ -350,7 +350,7 @@ Should return a `blockingResponse`.
 
 * `filter` Object
   * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
-                          will be filtered out.
+                   will be filtered out.
 * `listener` Function
   * `details` Object
     * `id` String - Request id.
@@ -381,7 +381,7 @@ Fired just before a request is going to be sent to the server, modifications of
 * `blockingResponse` Object
   * `cancel` Boolean - Whether to continue or block the request.
   * `responseHeaders` Object **optional** - When provided, the server is assumed to have
-                                                                      responded with these headers.
+                                            responded with these headers.
 
 Fired when HTTP response headers of a request have been received. Should return a
 `blockingResponse`.
@@ -430,7 +430,7 @@ Fired when a server initiated redirect is about to occur.
 
 * `filter` Object
   * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
-                          will be filtered out.
+                   will be filtered out.
 * `listener` Function
   * `details` Object
     * `id` String - Request id.
@@ -449,7 +449,7 @@ Fired when a request is completed.
 
 * `filter` Object
   * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
-                          will be filtered out.
+                   will be filtered out.
 * `listener` Function
   * `details` Object
     * `id` String - Request id.