Merge branch 'webrequest_upload_data_patch' of https://github.com/deepak1556/atom-shell into deepak1556-webrequest_upload_data_patch
This commit is contained in:
		
				commit
				
					
						30ff18ac64
					
				
			
		
					 5 changed files with 88 additions and 29 deletions
				
			
		| 
						 | 
					@ -75,6 +75,10 @@ void ToDictionary(base::DictionaryValue* details, net::URLRequest* request) {
 | 
				
			||||||
  details->SetString("resourceType",
 | 
					  details->SetString("resourceType",
 | 
				
			||||||
                     info ? ResourceTypeToString(info->GetResourceType())
 | 
					                     info ? ResourceTypeToString(info->GetResourceType())
 | 
				
			||||||
                          : "other");
 | 
					                          : "other");
 | 
				
			||||||
 | 
					  scoped_ptr<base::ListValue> list(new base::ListValue);
 | 
				
			||||||
 | 
					  GetUploadData(list.get(), request);
 | 
				
			||||||
 | 
					  if (!list->empty())
 | 
				
			||||||
 | 
					    details->Set("uploadData", list.Pass());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ToDictionary(base::DictionaryValue* details,
 | 
					void ToDictionary(base::DictionaryValue* details,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,7 @@
 | 
				
			||||||
#include "atom/common/node_includes.h"
 | 
					#include "atom/common/node_includes.h"
 | 
				
			||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
 | 
					#include "atom/common/native_mate_converters/gurl_converter.h"
 | 
				
			||||||
#include "atom/common/native_mate_converters/value_converter.h"
 | 
					#include "atom/common/native_mate_converters/value_converter.h"
 | 
				
			||||||
 | 
					#include "base/values.h"
 | 
				
			||||||
#include "native_mate/dictionary.h"
 | 
					#include "native_mate/dictionary.h"
 | 
				
			||||||
#include "net/base/upload_bytes_element_reader.h"
 | 
					#include "net/base/upload_bytes_element_reader.h"
 | 
				
			||||||
#include "net/base/upload_data_stream.h"
 | 
					#include "net/base/upload_data_stream.h"
 | 
				
			||||||
| 
						 | 
					@ -24,35 +25,15 @@ namespace mate {
 | 
				
			||||||
// static
 | 
					// static
 | 
				
			||||||
v8::Local<v8::Value> Converter<const net::URLRequest*>::ToV8(
 | 
					v8::Local<v8::Value> Converter<const net::URLRequest*>::ToV8(
 | 
				
			||||||
    v8::Isolate* isolate, const net::URLRequest* val) {
 | 
					    v8::Isolate* isolate, const net::URLRequest* val) {
 | 
				
			||||||
  mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
 | 
					  scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
 | 
				
			||||||
  dict.Set("method", val->method());
 | 
					  dict->SetString("method", val->method());
 | 
				
			||||||
  dict.Set("url", val->url().spec());
 | 
					  dict->SetStringWithoutPathExpansion("url", val->url().spec());
 | 
				
			||||||
  dict.Set("referrer", val->referrer());
 | 
					  dict->SetString("referrer", val->referrer());
 | 
				
			||||||
  const net::UploadDataStream* upload_data = val->get_upload();
 | 
					  scoped_ptr<base::ListValue> list(new base::ListValue);
 | 
				
			||||||
  if (upload_data) {
 | 
					  atom::GetUploadData(list.get(), val);
 | 
				
			||||||
    const ScopedVector<net::UploadElementReader>* readers =
 | 
					  if (!list->empty())
 | 
				
			||||||
        upload_data->GetElementReaders();
 | 
					    dict->Set("uploadData", list.Pass());
 | 
				
			||||||
    std::vector<mate::Dictionary> upload_data_list;
 | 
					  return mate::ConvertToV8(isolate, *(dict.get()));
 | 
				
			||||||
    upload_data_list.reserve(readers->size());
 | 
					 | 
				
			||||||
    for (const auto& reader : *readers) {
 | 
					 | 
				
			||||||
      auto upload_data_dict = mate::Dictionary::CreateEmpty(isolate);
 | 
					 | 
				
			||||||
      if (reader->AsBytesReader()) {
 | 
					 | 
				
			||||||
        const net::UploadBytesElementReader* bytes_reader =
 | 
					 | 
				
			||||||
            reader->AsBytesReader();
 | 
					 | 
				
			||||||
        auto bytes =
 | 
					 | 
				
			||||||
            node::Buffer::Copy(isolate, bytes_reader->bytes(),
 | 
					 | 
				
			||||||
                               bytes_reader->length()).ToLocalChecked();
 | 
					 | 
				
			||||||
        upload_data_dict.Set("bytes", bytes);
 | 
					 | 
				
			||||||
      } else if (reader->AsFileReader()) {
 | 
					 | 
				
			||||||
        const net::UploadFileElementReader* file_reader =
 | 
					 | 
				
			||||||
            reader->AsFileReader();
 | 
					 | 
				
			||||||
        upload_data_dict.Set("file", file_reader->path().AsUTF8Unsafe());
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      upload_data_list.push_back(upload_data_dict);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    dict.Set("uploadData", upload_data_list);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  return mate::ConvertToV8(isolate, dict);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// static
 | 
					// static
 | 
				
			||||||
| 
						 | 
					@ -83,3 +64,34 @@ v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}  // namespace mate
 | 
					}  // namespace mate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace atom {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void GetUploadData(base::ListValue* upload_data_list,
 | 
				
			||||||
 | 
					                   const net::URLRequest* request) {
 | 
				
			||||||
 | 
					  const net::UploadDataStream* upload_data = request->get_upload();
 | 
				
			||||||
 | 
					  if (!upload_data)
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  const ScopedVector<net::UploadElementReader>* readers =
 | 
				
			||||||
 | 
					      upload_data->GetElementReaders();
 | 
				
			||||||
 | 
					  for (const auto& reader : *readers) {
 | 
				
			||||||
 | 
					    scoped_ptr<base::DictionaryValue> upload_data_dict(
 | 
				
			||||||
 | 
					        new base::DictionaryValue);
 | 
				
			||||||
 | 
					    if (reader->AsBytesReader()) {
 | 
				
			||||||
 | 
					      const net::UploadBytesElementReader* bytes_reader =
 | 
				
			||||||
 | 
					          reader->AsBytesReader();
 | 
				
			||||||
 | 
					      scoped_ptr<base::Value> bytes(
 | 
				
			||||||
 | 
					          base::BinaryValue::CreateWithCopiedBuffer(bytes_reader->bytes(),
 | 
				
			||||||
 | 
					                                                    bytes_reader->length()));
 | 
				
			||||||
 | 
					      upload_data_dict->Set("bytes", bytes.Pass());
 | 
				
			||||||
 | 
					    } else if (reader->AsFileReader()) {
 | 
				
			||||||
 | 
					      const net::UploadFileElementReader* file_reader =
 | 
				
			||||||
 | 
					          reader->AsFileReader();
 | 
				
			||||||
 | 
					      auto file_path = file_reader->path().AsUTF8Unsafe();
 | 
				
			||||||
 | 
					      upload_data_dict->SetStringWithoutPathExpansion("file", file_path);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    upload_data_list->Append(upload_data_dict.Pass());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}  // namespace atom
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,6 +8,10 @@
 | 
				
			||||||
#include "base/memory/ref_counted.h"
 | 
					#include "base/memory/ref_counted.h"
 | 
				
			||||||
#include "native_mate/converter.h"
 | 
					#include "native_mate/converter.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace base {
 | 
				
			||||||
 | 
					class ListValue;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace net {
 | 
					namespace net {
 | 
				
			||||||
class AuthChallengeInfo;
 | 
					class AuthChallengeInfo;
 | 
				
			||||||
class URLRequest;
 | 
					class URLRequest;
 | 
				
			||||||
| 
						 | 
					@ -36,4 +40,11 @@ struct Converter<scoped_refptr<net::X509Certificate>> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}  // namespace mate
 | 
					}  // namespace mate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace atom {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void GetUploadData(base::ListValue* upload_data_list,
 | 
				
			||||||
 | 
					                   const net::URLRequest* request);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}  // namespace atom
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif  // ATOM_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_
 | 
					#endif  // ATOM_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -332,6 +332,9 @@ is about to occur.
 | 
				
			||||||
  * `method` String
 | 
					  * `method` String
 | 
				
			||||||
  * `resourceType` String
 | 
					  * `resourceType` String
 | 
				
			||||||
  * `timestamp` Double
 | 
					  * `timestamp` Double
 | 
				
			||||||
 | 
					  * `uploadData` Array (optional)
 | 
				
			||||||
 | 
					    * `bytes` Buffer - Content being sent.
 | 
				
			||||||
 | 
					    * `file` String - Path of file being uploaded.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The `callback` has to be called with an `response` object:
 | 
					The `callback` has to be called with an `response` object:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
const assert = require('assert');
 | 
					const assert = require('assert');
 | 
				
			||||||
const http = require('http');
 | 
					const http = require('http');
 | 
				
			||||||
 | 
					const qs = require('querystring');
 | 
				
			||||||
const remote = require('electron').remote;
 | 
					const remote = require('electron').remote;
 | 
				
			||||||
const session = remote.session;
 | 
					const session = remote.session;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,6 +84,7 @@ describe('webRequest module', function() {
 | 
				
			||||||
        assert.equal(details.url, defaultURL);
 | 
					        assert.equal(details.url, defaultURL);
 | 
				
			||||||
        assert.equal(details.method, 'GET');
 | 
					        assert.equal(details.method, 'GET');
 | 
				
			||||||
        assert.equal(details.resourceType, 'xhr');
 | 
					        assert.equal(details.resourceType, 'xhr');
 | 
				
			||||||
 | 
					        assert(!details.uploadData);
 | 
				
			||||||
        return callback({});
 | 
					        return callback({});
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
      return $.ajax({
 | 
					      return $.ajax({
 | 
				
			||||||
| 
						 | 
					@ -96,6 +98,33 @@ describe('webRequest module', function() {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					    it('receives post data in details object', function(done) {
 | 
				
			||||||
 | 
					      var postData = {
 | 
				
			||||||
 | 
					        name: 'post test',
 | 
				
			||||||
 | 
					        type: 'string'
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					      ses.webRequest.onBeforeRequest(function(details, callback) {
 | 
				
			||||||
 | 
					        var data;
 | 
				
			||||||
 | 
					        assert.equal(details.url, defaultURL);
 | 
				
			||||||
 | 
					        assert.equal(details.method, 'POST');
 | 
				
			||||||
 | 
					        assert.equal(details.uploadData.length, 1);
 | 
				
			||||||
 | 
					        data = qs.parse(details.uploadData[0].bytes.toString());
 | 
				
			||||||
 | 
					        assert.deepEqual(data, postData);
 | 
				
			||||||
 | 
					        return callback({
 | 
				
			||||||
 | 
					          cancel: true
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					      return $.ajax({
 | 
				
			||||||
 | 
					        url: defaultURL,
 | 
				
			||||||
 | 
					        type: 'POST',
 | 
				
			||||||
 | 
					        data: postData,
 | 
				
			||||||
 | 
					        success: function() {
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        error: function() {
 | 
				
			||||||
 | 
					          done();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
    return it('can redirect the request', function(done) {
 | 
					    return it('can redirect the request', function(done) {
 | 
				
			||||||
      ses.webRequest.onBeforeRequest(function(details, callback) {
 | 
					      ses.webRequest.onBeforeRequest(function(details, callback) {
 | 
				
			||||||
        if (details.url === defaultURL) {
 | 
					        if (details.url === defaultURL) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue