Hello,
does WinCCOA Control manager supports http basic authorization header?
If yes, please help me send simple http get/post (cgi command) that requires http basic authorization.
I would appreciate very much an example of such command.
I intend to make control script that will send http cgi-bin command to camera. Those cgi commands requires basic authorization.
Here is an example of http command that I should send:
http:///cgi-bin/admin/setparam.cgi?system_reset=0
user:pass should be added to this command as http basic authorization header.
Best regards,
Vedad Ramovic
Htpp basic authorization
- mkoller
- Posts:741
- Joined: Fri Sep 17, 2010 9:03 am
Re: Htpp basic authorization
HTTP is defined in RFC-2616, e.g.: http://tools.ietf.org/html/rfc2616,
Basic Authentication here: http://tools.ietf.org/html/rfc2617
The Basic authentication header looks like this:
Authorization: Basic
What we currently do not provide as a CTRL function is to encode a blob to base64, so you would need to implement this on your own
or if you need this currently only for exactly one user/password combination, you can simply calculate this externally and put the resulting string
into the CTRL script.
E.g. on Linux call: echo -n "Aladdin:open sesame"|base64
which gets you "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
So the HTTP header needs to look like this:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Don't forget to terminate each header line with \\r\\n
Basic Authentication here: http://tools.ietf.org/html/rfc2617
The Basic authentication header looks like this:
Authorization: Basic
What we currently do not provide as a CTRL function is to encode a blob to base64, so you would need to implement this on your own
or if you need this currently only for exactly one user/password combination, you can simply calculate this externally and put the resulting string
into the CTRL script.
E.g. on Linux call: echo -n "Aladdin:open sesame"|base64
which gets you "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
So the HTTP header needs to look like this:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Don't forget to terminate each header line with \\r\\n
- Gertjan van Schijndel
- Posts:634
- Joined: Mon Aug 02, 2010 10:37 am
Re: Htpp basic authorization
I have used this code to encode the user:pass combination:
Code: Select all
#uses "CtrlXmlRpc" // xmlrpcEncodeValue is used for base64 encoding, so we need to load this
string base64encode(string text, bool utf8 = false)
{
blob input;
string result;
blobAppendValue(input, text, strlen(text));
int rc = xmlrpcEncodeValue(input, result, utf8);
// Remove leading '' part
result = substr(result, strlen(""));
// And trailing '' part
result = substr(result, 0, strlen(result) - strlen(""));
DebugFTN("BASE64", "base64encode(" + text + ", " + utf8 + ") input: " + input + " rc: " + rc + " result: " + result);
return result;
}- vedadramovic
- Posts:121
- Joined: Mon Apr 07, 2014 10:36 am
Re: Htpp basic authorization
Thank you Martin and Gertjan,
do you have any example of control script that sends http request along with basic authorization?
I understud how to make base64 encoding.
Best regards,
Vedad Ramovic
do you have any example of control script that sends http request along with basic authorization?
I understud how to make base64 encoding.
Best regards,
Vedad Ramovic
- Gertjan van Schijndel
- Posts:634
- Joined: Mon Aug 02, 2010 10:37 am
Re: Http basic authorization
Here is an example of a http post with basic authorization:
Code: Select all
// General HTTP constants
string HTTP_HOST = "Host:";
string HTTP_PROTOCOL = "HTTP/";
string HTTP_VERSION = "1.1";
string HTTP_COMMAND_GET = "GET";
string HTTP_COMMAND_POST = "POST";
string HTTP_CONTENT_TYPE = "Content-Type:";
string HTTP_CONTENT_LENGTH = "Content-Length:";
string HTTP_CONTENT_ENCODING = "Content-Transfer-Encoding:";
string HTTP_CONNECTION = "Connection:";
string HTTP_CONNECTION_TYPE = "Keep-Alive";
string HTTP_TRANSFER_ENCODING = "Transfer-Encoding:";
string HTTP_WWW_AUTHENICATE = "WWW-Authenticate:";
string HTTP_AUTHORIZATION = "Authorization:";
string LINE_TERMINATOR = "\\n";
int httpPost(int handle, string host, string uri, const string &content, string contentType, string username = "", string password = "", string authorization = "Basic")
{
// When an username is provided first try it with basic authorization
if (authorization == "Basic" && username != "")
{
authorization = "Basic " + base64encode(username + ":" + password);
}
string message = HTTP_COMMAND_POST + " " + uri + " " + HTTP_PROTOCOL + HTTP_VERSION + LINE_TERMINATOR +
HTTP_CONTENT_TYPE + " " + contentType + LINE_TERMINATOR +
HTTP_HOST + " " + host + LINE_TERMINATOR +
(authorization == "" ? "" : HTTP_AUTHORIZATION + " " + authorization + LINE_TERMINATOR) +
HTTP_CONTENT_LENGTH + " " + strlen(content) + LINE_TERMINATOR +
HTTP_CONNECTION + " " + HTTP_CONNECTION_TYPE + LINE_TERMINATOR +
LINE_TERMINATOR + content;
int bytesSent = tcpWrite(handle, message);
dyn_errClass errors = getLastError();
if (dynlen(errors) > 0)
{
throwError(makeError("", PRIO_WARNING, ERR_SYSTEM, 54, "httpPost(" + handle + ", " + host + ", " + uri + ", ...) error during tcpWrite", bytesSent,
HTTP_COMMAND_POST + " " + uri + " " + HTTP_PROTOCOL + HTTP_VERSION + " " + HTTP_CONTENT_LENGTH + " " + strlen(content)));
throwError(errors);
return -1;
}
return bytesSent;
}
- mkoller
- Posts:741
- Joined: Fri Sep 17, 2010 9:03 am
Re: Http basic authorization
The LINE_TERMINATOR is wrong.
RFC 2616 defines this to always be "\\r\\n": "The line terminator for message-header fields is the sequence CRLF"
RFC 2616 defines this to always be "\\r\\n": "The line terminator for message-header fields is the sequence CRLF"
- vedadramovic
- Posts:121
- Joined: Mon Apr 07, 2014 10:36 am
Re: Htpp basic authorization
Thank you very much Gertjan and Martin.
You helped me very much.
In case someone meet similar request here is what worked for me.
#uses "CtrlHTTP"
#uses "CtrlXmlRpc" // xmlrpcEncodeValue is used for base64 encoding, so we need to load this
// General HTTP constants
string HTTP_HOST = "Host:";
string HTTP_PROTOCOL = "HTTP/";
string HTTP_VERSION = "1.1";
string HTTP_COMMAND_GET = "GET";
string HTTP_COMMAND_POST = "POST";
string HTTP_CONTENT_TYPE = "Content-Type:";
string HTTP_CONTENT_LENGTH = "Content-Length:";
string HTTP_CONTENT_ENCODING = "Content-Transfer-Encoding:";
string HTTP_CONNECTION = "Connection:";
string HTTP_CONNECTION_TYPE = "Keep-Alive";
string HTTP_TRANSFER_ENCODING = "Transfer-Encoding:";
string HTTP_WWW_AUTHENICATE = "WWW-Authenticate:";
string HTTP_AUTHORIZATION = "Authorization:";
string LINE_TERMINATOR = "\\r\\n";
main()
{
RebootCamera();
}
RebootCamera()
{
int read, open, close, err;
string data;
unsigned port;
port=80;
open=tcpOpen("192.168.134.201", port);
DebugTN(open);
httpGet(open, "192.168.134.201", "/cgi-bin/admin/setparam.cgi?system_reset=0", "root", "nik");
close=tcpClose(open);
err=getLastError();
DebugTN("close: " + close);
DebugTN("err:" +err);
}
int httpGet(int handle, string host, string uri, string username = "", string password = "", string authorization = "Basic")
{
// When an username is provided first try it with basic authorization
if (authorization == "Basic" && username != "")
{
authorization = "Basic " + base64encode(username + ":" + password);
}
string message = HTTP_COMMAND_GET + " " + uri + " " + HTTP_PROTOCOL + HTTP_VERSION + LINE_TERMINATOR +
HTTP_HOST + " " + host + LINE_TERMINATOR +
(authorization == "" ? "" : HTTP_AUTHORIZATION + " " + authorization + LINE_TERMINATOR) +
HTTP_CONNECTION + " " + HTTP_CONNECTION_TYPE + LINE_TERMINATOR +
LINE_TERMINATOR;//+
int bytesSent = tcpWrite(handle, message);
dyn_errClass errors = getLastError();
if (dynlen(errors) > 0)
{
throwError(makeError("", PRIO_WARNING, ERR_SYSTEM, 54, "httpGet(" + handle + ", " + host + ", " + uri + ", ...) error during tcpWrite", bytesSent,
HTTP_COMMAND_GET + " " + uri + " " + HTTP_PROTOCOL + HTTP_VERSION));
throwError(errors);
return -1;
}
return bytesSent;
}
string base64encode(string text, bool utf8 = false)
{
blob input;
string result;
blobAppendValue(input, text, strlen(text));
int rc = xmlrpcEncodeValue(input, result, utf8);
// Remove leading '' part
result = substr(result, strlen(""));
// And trailing '' part
result = substr(result, 0, strlen(result) - strlen(""));
DebugFTN("BASE64", "base64encode(" + text + ", " + utf8 + ") input: " + input + " rc: " + rc + " result: " + result);
return result;
}
You helped me very much.
In case someone meet similar request here is what worked for me.
#uses "CtrlHTTP"
#uses "CtrlXmlRpc" // xmlrpcEncodeValue is used for base64 encoding, so we need to load this
// General HTTP constants
string HTTP_HOST = "Host:";
string HTTP_PROTOCOL = "HTTP/";
string HTTP_VERSION = "1.1";
string HTTP_COMMAND_GET = "GET";
string HTTP_COMMAND_POST = "POST";
string HTTP_CONTENT_TYPE = "Content-Type:";
string HTTP_CONTENT_LENGTH = "Content-Length:";
string HTTP_CONTENT_ENCODING = "Content-Transfer-Encoding:";
string HTTP_CONNECTION = "Connection:";
string HTTP_CONNECTION_TYPE = "Keep-Alive";
string HTTP_TRANSFER_ENCODING = "Transfer-Encoding:";
string HTTP_WWW_AUTHENICATE = "WWW-Authenticate:";
string HTTP_AUTHORIZATION = "Authorization:";
string LINE_TERMINATOR = "\\r\\n";
main()
{
RebootCamera();
}
RebootCamera()
{
int read, open, close, err;
string data;
unsigned port;
port=80;
open=tcpOpen("192.168.134.201", port);
DebugTN(open);
httpGet(open, "192.168.134.201", "/cgi-bin/admin/setparam.cgi?system_reset=0", "root", "nik");
close=tcpClose(open);
err=getLastError();
DebugTN("close: " + close);
DebugTN("err:" +err);
}
int httpGet(int handle, string host, string uri, string username = "", string password = "", string authorization = "Basic")
{
// When an username is provided first try it with basic authorization
if (authorization == "Basic" && username != "")
{
authorization = "Basic " + base64encode(username + ":" + password);
}
string message = HTTP_COMMAND_GET + " " + uri + " " + HTTP_PROTOCOL + HTTP_VERSION + LINE_TERMINATOR +
HTTP_HOST + " " + host + LINE_TERMINATOR +
(authorization == "" ? "" : HTTP_AUTHORIZATION + " " + authorization + LINE_TERMINATOR) +
HTTP_CONNECTION + " " + HTTP_CONNECTION_TYPE + LINE_TERMINATOR +
LINE_TERMINATOR;//+
int bytesSent = tcpWrite(handle, message);
dyn_errClass errors = getLastError();
if (dynlen(errors) > 0)
{
throwError(makeError("", PRIO_WARNING, ERR_SYSTEM, 54, "httpGet(" + handle + ", " + host + ", " + uri + ", ...) error during tcpWrite", bytesSent,
HTTP_COMMAND_GET + " " + uri + " " + HTTP_PROTOCOL + HTTP_VERSION));
throwError(errors);
return -1;
}
return bytesSent;
}
string base64encode(string text, bool utf8 = false)
{
blob input;
string result;
blobAppendValue(input, text, strlen(text));
int rc = xmlrpcEncodeValue(input, result, utf8);
// Remove leading '' part
result = substr(result, strlen(""));
// And trailing '' part
result = substr(result, 0, strlen(result) - strlen(""));
DebugFTN("BASE64", "base64encode(" + text + ", " + utf8 + ") input: " + input + " rc: " + rc + " result: " + result);
return result;
}