Skip to content
Snippets Groups Projects
Commit 7ad9c4bd authored by deuce's avatar deuce
Browse files

Move stuff from letsyncrypt.js to http.js

Parsing the response code and headers is an HTTP thing, not an ACME thing.
parent 29063c8b
Branches
Tags
No related merge requests found
...@@ -78,7 +78,7 @@ ACMEv2.prototype.get_directory = function() ...@@ -78,7 +78,7 @@ ACMEv2.prototype.get_directory = function()
{ {
if (this.directory === undefined) { if (this.directory === undefined) {
var ret = this.ua.Get("https://"+this.host+this.dir_path); var ret = this.ua.Get("https://"+this.host+this.dir_path);
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
throw("Error fetching directory"); throw("Error fetching directory");
this.store_headers(true); this.store_headers(true);
this.directory = JSON.parse(ret); this.directory = JSON.parse(ret);
...@@ -94,7 +94,7 @@ ACMEv2.prototype.create_new_account = function(opts) ...@@ -94,7 +94,7 @@ ACMEv2.prototype.create_new_account = function(opts)
{ {
var ret = this.post('newAccount', opts); var ret = this.post('newAccount', opts);
if (this.get_response_code() != 201) if (this.ua.response_code != 201)
throw("newAccount did not return a 201 status!"); throw("newAccount did not return a 201 status!");
if (this.last_headers['Location'] === undefined) if (this.last_headers['Location'] === undefined)
...@@ -110,9 +110,9 @@ ACMEv2.prototype.create_new_order = function(opts) ...@@ -110,9 +110,9 @@ ACMEv2.prototype.create_new_order = function(opts)
if (opts.identifiers === undefined) if (opts.identifiers === undefined)
throw("create_new_order() requires an identifier in opts"); throw("create_new_order() requires an identifier in opts");
ret = JSON.parse(this.post('newOrder', opts)); ret = JSON.parse(this.post('newOrder', opts));
if (this.get_response_code() != 201) { if (this.ua.response_code != 201) {
log(LOG_DEBUG, JSON.stringify(ret)); log(LOG_DEBUG, JSON.stringify(ret));
throw("newOrder responded with "+this.get_response_code()+" not 201"); throw("newOrder responded with "+this.ua.response_code+" not 201");
} }
if (this.last_headers['Location'] === undefined) if (this.last_headers['Location'] === undefined)
...@@ -126,7 +126,7 @@ ACMEv2.prototype.accept_challenge = function(challenge) ...@@ -126,7 +126,7 @@ ACMEv2.prototype.accept_challenge = function(challenge)
{ {
var opts={keyAuthorization:challenge.token+"."+this.thumbprint()}; var opts={keyAuthorization:challenge.token+"."+this.thumbprint()};
var ret = this.post_url(challenge.url, opts) var ret = this.post_url(challenge.url, opts)
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
throw("accept_challenge did not return 200"); throw("accept_challenge did not return 200");
return JSON.parse(ret); return JSON.parse(ret);
} }
...@@ -135,7 +135,7 @@ ACMEv2.prototype.poll_authorization = function(auth) ...@@ -135,7 +135,7 @@ ACMEv2.prototype.poll_authorization = function(auth)
{ {
var ret = JSON.parse(this.ua.Get(auth)); var ret = JSON.parse(this.ua.Get(auth));
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
return false; return false;
this.store_headers(true); this.store_headers(true);
...@@ -159,7 +159,7 @@ ACMEv2.prototype.finalize_order = function(order, csr) ...@@ -159,7 +159,7 @@ ACMEv2.prototype.finalize_order = function(order, csr)
opts.csr = this.base64url(csr.export(CryptCert.FORMAT.CERTIFICATE)); opts.csr = this.base64url(csr.export(CryptCert.FORMAT.CERTIFICATE));
var ret = this.post_url(order.finalize, opts) var ret = this.post_url(order.finalize, opts)
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
throw("finalize_order did not return 200"); throw("finalize_order did not return 200");
return JSON.parse(ret); return JSON.parse(ret);
...@@ -171,7 +171,7 @@ ACMEv2.prototype.poll_order = function(order) ...@@ -171,7 +171,7 @@ ACMEv2.prototype.poll_order = function(order)
if (loc === undefined) if (loc === undefined)
throw("No order location!"); throw("No order location!");
var ret = this.ua.Get(loc) var ret = this.ua.Get(loc)
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
throw("order poll did not return 200"); throw("order poll did not return 200");
this.store_headers(true); this.store_headers(true);
...@@ -185,7 +185,7 @@ ACMEv2.prototype.get_cert = function(order) ...@@ -185,7 +185,7 @@ ACMEv2.prototype.get_cert = function(order)
if (order.certificate === undefined) if (order.certificate === undefined)
throw("Order has no certificate!"); throw("Order has no certificate!");
var cert = this.ua.Get(order.certificate); var cert = this.ua.Get(order.certificate);
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
throw("get_cert request did not return 200"); throw("get_cert request did not return 200");
this.store_headers(true); this.store_headers(true);
return new CryptCert(cert); return new CryptCert(cert);
...@@ -222,7 +222,7 @@ ACMEv2.prototype.get_authorization = function(url) ...@@ -222,7 +222,7 @@ ACMEv2.prototype.get_authorization = function(url)
{ {
var ret = this.ua.Get(url); var ret = this.ua.Get(url);
if (this.get_response_code() != 200) if (this.ua.response_code != 200)
throw("get_authorization request did not return 200"); throw("get_authorization request did not return 200");
this.store_headers(true); this.store_headers(true);
...@@ -272,21 +272,12 @@ ACMEv2.prototype.get_response_code = function() ...@@ -272,21 +272,12 @@ ACMEv2.prototype.get_response_code = function()
return parseInt(m[1], 10); return parseInt(m[1], 10);
} }
// TODO: Should this be in http.js?
ACMEv2.prototype.store_headers = function(update_nonce) ACMEv2.prototype.store_headers = function(update_nonce)
{ {
var h = {}; var h = this.ua.response_headers_parsed;
if (update_nonce === undefined) if (update_nonce === undefined)
update_nonce = false; update_nonce = false;
for(i in this.ua.response_headers) {
m = this.ua.response_headers[i].match(/^(.*?):\s*(.*?)\s*$/);
if (m) {
if (h[m[1]] == undefined)
h[m[1]] = [];
h[m[1]].push(m[2]);
}
}
if (h['Replay-Nonce'] !== undefined) { if (h['Replay-Nonce'] !== undefined) {
if (update_nonce) if (update_nonce)
this.last_nonce = h['Replay-Nonce'][0]; this.last_nonce = h['Replay-Nonce'][0];
...@@ -339,9 +330,8 @@ ACMEv2.prototype.post_url = function(url, data, post_method) ...@@ -339,9 +330,8 @@ ACMEv2.prototype.post_url = function(url, data, post_method)
body = body.replace(/:{/g, ': {'); body = body.replace(/:{/g, ': {');
body = body.replace(/,"/g, ', "'); body = body.replace(/,"/g, ', "');
ret = this.ua.Post(url, body); ret = this.ua.Post(url, body);
var resp = this.get_response_code();
/* We leave error handling to the caller */ /* We leave error handling to the caller */
if (resp == 200 || resp == 201) if (this.ua.response_code == 200 || this.ua.response_code == 201)
this.store_headers(true); this.store_headers(true);
return ret; return ret;
} }
...@@ -19,7 +19,7 @@ function HTTPRequest(username,password,extra_headers) ...@@ -19,7 +19,7 @@ function HTTPRequest(username,password,extra_headers)
this.base; this.base;
this.url; this.url;
this.body; this.body;
this.username=username; this.username=username;
this.password=password; this.password=password;
...@@ -130,12 +130,17 @@ function HTTPRequest(username,password,extra_headers) ...@@ -130,12 +130,17 @@ function HTTPRequest(username,password,extra_headers)
this.status_line=this.sock.recvline(4096); this.status_line=this.sock.recvline(4096);
if(this.status_line==null) if(this.status_line==null)
throw("Unable to read status"); throw("Unable to read status");
var m = this.status_line.match(/^HTTP\/[0-9]+\.[0-9]+ ([0-9]{3})/);
if (m === null)
throw("Unable to parse status line '"+this.status_line+"'");
this.response_code = parseInt(m[1], 10);
}; };
this.ReadHeaders=function() { this.ReadHeaders=function() {
var header=''; var header='';
var m; var m;
this.response_headers=[]; this.response_headers=[];
this.response_headers_parsed={};
for(;;) { for(;;) {
header=this.sock.recvline(4096, 120); header=this.sock.recvline(4096, 120);
...@@ -147,6 +152,12 @@ function HTTPRequest(username,password,extra_headers) ...@@ -147,6 +152,12 @@ function HTTPRequest(username,password,extra_headers)
m=header.match(/^Content-length:\s+([0-9]+)$/i); m=header.match(/^Content-length:\s+([0-9]+)$/i);
if(m!=null) if(m!=null)
this.contentlength=parseInt(m[1]); this.contentlength=parseInt(m[1]);
m = header.match(/^(.*?):\s*(.*?)\s*$/);
if (m) {
if (this.response_headers_parsed[m[1]] == undefined)
this.response_headers_parsed[m[1]] = [];
this.response_headers_parsed[m[1]].push(m[2]);
}
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment