Re: VibeD - REST API and vibed.web.auth framework
Thank you for explanation. It fix my problem with compilation. I was using https://github.com/rejectedsoftware/vibe.d/blob/master/examples/web-auth/source/app.d as example and there is @safe function. I try to compile it with newest (beta) version of vibe.d and it compiled too (so i suspect in newest version system function is marked as trusted/safe too or it is fixed some other way :)). Regards holo
VibeD - REST API and vibed.web.auth framework
Hello I'm trying to use auth framework with REST api ( http://vibed.org/api/vibe.web.auth/ ). Is it possible to use it with registerRestInterface? According to description under: http://vibed.org/api/vibe.web.auth/requiresAuth it should be available on both Web and REST. Here is my example code and compilation errors bellow: import vibe.d; import vibe.web.auth; import vibe.http.session; import vibe.web.rest : registerRestInterface; import vibe.web.web : noRoute; import std.algorithm, std.array; struct AuthInfo { string userName; bool premium; bool admin; @safe: bool isAdmin() { return this.admin; } bool isPremiumUser() { return this.premium; } } interface IfOAuthAPI { void postLogin(ValidUsername login, ValidPassword password); //bool postTokenValidation(string token, string userName); } @path("/api") @requiresAuth class OAuthAPI : IfOAuthAPI { @noRoute AuthInfo authenticate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe { if (!req.session || !req.session.isKeySet("auth")) throw new HTTPStatusException(HTTPStatus.forbidden, "Not authorized to perform this action!"); return req.session.get!AuthInfo("auth"); } this(MongoCollection coll) { collection = coll; } private: MongoCollection collection; public: @noAuth string postLogin(ValidUsername login, ValidPassword password) { logInfo("Recived data for" ~ login ~ "and" ~ password); Bson query = Bson(["username" : Bson(login)]); auto result = collection.find(query); foreach (i, doc; result) logInfo("Item %d: %s", i, doc.toJson().toString()); logInfo("Sending respond"); return "OK"; } } And im getting such errors: ms-frontpage ~master: building configuration "application"... ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead. source/app.d(14,31): Error: cannot create instance of interface IfOAuthAPI source/service/oauth.d(35,8): Error: @safe function 'oauth.OAuthAPI.authenticate' cannot call @system function 'vibe.http.session.Session.opCast' source/service/oauth.d(35,44): Error: @safe function 'oauth.OAuthAPI.authenticate' cannot call @system function 'vibe.http.session.Session.isKeySet' source/service/oauth.d(36,10): Error: @safe function 'oauth.OAuthAPI.authenticate' cannot call @system constructor 'vibe.http.common.HTTPStatusException.this' source/service/oauth.d(38,34): Error: @safe function 'oauth.OAuthAPI.authenticate' cannot call @system function 'vibe.http.session.Session.get!(AuthInfo).get' dmd failed with exit code 1. Regards holo
Re: VibeD - takeing out value from json
There was need to change one line from: auto header = result["heading"].to!string; to: auto header = result[0]["heading"].to!string;
VibeD - takeing out value from json
Hello I wrote such code to test restAPI client: /// //app.d /// import vibe.d; import sites.frontpage; import msfrontpage; shared static this() { // the router will match incoming HTTP requests to the proper routes auto router = new URLRouter; // registers each method of WebChat in the router auto api = new RestInterfaceClient!IfFrontPageAPI("http://127.0.0.1:8090/";); router.registerWebInterface(new SilesianCloud(api)); // match incoming requests to files in the public/ folder router.get("*", serveStaticFiles("public/")); auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; listenHTTP(settings, router); logInfo("Please open http://127.0.0.1:8080/ in your browser."); } // //frontpage.d // module sites.frontpage; import vibe.d; import msfrontpage; final class SilesianCloud { this(RestInterfaceClient!(IfFrontPageAPI) tmp) { this.frontpageapi = tmp; } public: // GET / void get() { Json result = frontpageapi.getHome(); logInfo(result.toString); auto header = result["heading"].get!string; logInfo(result["heading"].get!string); render!("frontpage.dt",header); } private: RestInterfaceClient!(IfFrontPageAPI) frontpageapi; } But it looks like my value is not "extracted" from Json in proper string, even logInfo(header) shows "nothing" and in web broser im getting: 500 - Internal Server Error Internal Server Error Internal error information: std.json.JSONException@std/json.d(1330): Got JSON of type array, expected object. ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/data/json.d:1054 const(pure @safe void function(immutable(char)[])) vibe.data.json.Json.checkType!(vibe.data.json.Json[immutable(char)[]]).checkType [0x88f762] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/data/json.d:339 ref vibe.data.json.Json vibe.data.json.Json.opIndex(immutable(char)[]) [0x977d4e] source/sites/frontpage.d:20 void sites.frontpage.SilesianCloud.get() [0x7d129c] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/web.d:822 void vibe.web.web.handleRequest!("get", void sites.frontpage.SilesianCloud.get(), sites.frontpage.SilesianCloud).handleRequest(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse, sites.frontpage.SilesianCloud, vibe.web.web.WebInterfaceSettings) [0x7bd220] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/web.d:185 void vibe.web.web.__T20registerWebInterfaceHTC5sites9frontpage13SilesianCloudVE4vibe3web6common11MethodStylei5Z.registerWebInterface(vibe.http.router.URLRouter, sites.frontpage.SilesianCloud, vibe.web.web.WebInterfaceSettings).__lambda4!(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse).__lambda4(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse) [0x7bca20] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:268 _D4vibe4http6router9URLRouter13handleRequestMFC4vibe4http6server17HTTPServerRequestC4vibe4http6server18HTTPServerResponseZ21__T9__lambda4TmTAAyaZ9__lambda4MFmMAAyaZb [0x864a0e] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:595 const(bool function(immutable(char)[], scope bool delegate(ulong, scope immutable(char)[][]))) vibe.http.router.MatchTree!(vibe.http.router.Route).MatchTree.doMatch [0x8657f5] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:528 bool vibe.http.router.MatchTree!(vibe.http.router.Route).MatchTree.match(immutable(char)[], scope bool delegate(ulong, scope immutable(char)[][])) [0x865047] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:261 void vibe.http.router.URLRouter.handleRequest(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse) [0x8646ad] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d:1981 bool vibe.http.server.handleRequest(vibe.core.stream.Stream, vibe.core.net.TCPConnection, vibe.http.server.HTTPListenInfo, ref vibe.http.server.HTTPServerSettings, ref bool) [0x8a7490] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d:1737 void vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection, vibe.http.server.HTTPListenInfo) [0x8a5a9a] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d:1618 void vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings).doListen(vibe.http.server.HTTPListenInfo, bool, bool).__lambda4(vibe.core.net.TCPConnection) [0x8a5538] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/drivers/libevent2_tcp.d:618 void vibe.core.drivers.libevent2_tcp.ClientTask.execute() [0x964098] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/core.d:595 void vibe.core.core.makeTaskFuncInfo!(void delegate()).makeTaskFuncInfo(ref void delegate()).callDelegate(vibe.core.core.TaskFuncInfo*) [0x81193f] ../../.dub/
VibeD - RestInterfaceClient - non-constant expression
Hello I'm trying to move forward with my code from here: https://forum.dlang.org/post/yftgpzlotuqzechcn...@forum.dlang.org I create my client app and trying to use RestInterfaceClient to connect to my api. Here is my present code for client: module sites.frontpage; import vibe.d; import msfrontpage; final class SilesianCloud { private: auto api = new RestInterfaceClient!IfFrontPageAPI("http://127.0.0.1:8090/api/";); public: // GET / void get() { Json result = api.getHome(); logInfo(result.toString); render!"frontpage.dt"; } } When i try to compile it im getting: ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead. Compiling Diet HTML template frontpage.dt... ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/inet/url.d(61,18): Error: non-constant expression "http://127.0.0.1:8090/api/"[0..4] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/inet/url.d(129,15): Error: non-constant expression "http://127.0.0.1:8090/api/"[21..26] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/inet/url.d(123,23): Error: non-constant expression "http://127.0.0.1:8090/api/"[7..16] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/internal/rest/common.d(136,29): Error: non-constant expression ['h', 't', 't', 'p', ':', '/', '/', '1', '2', '7', '.', '0', '.', '0', '.', '1', ':', '8', '0', '9', '0', '/', 'a', 'p', 'i', '/', 'a', 'p', 'i'][0..29] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/internal/rest/common.d(156,46): Error: non-constant expression ['h', 'o', 'm', 'e'][0..4] ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/internal/rest/common.d(156,46): Error: non-constant expression ['h', 'o', 'm', 'e'][0..4] dmd failed with exit code 1. What did i miss? I was trying to do it according to vibed documentation: http://vibed.org/api/vibe.web.rest/RestInterfaceClient
Re: VibeD - undefinded identifier
On Sunday, 23 July 2017 at 17:16:44 UTC, Seb wrote: On Sunday, 23 July 2017 at 16:37:19 UTC, holo wrote: On Sunday, 23 July 2017 at 16:27:40 UTC, Mike Parker wrote: On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote: this(auto tmp) You need to specify a type here instead of using auto. Thanks for reply i fix it. But right now im getting: ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead. source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are: /usr/include/dlang/dmd/std/array.d(419,6): std.array.byPair(Key, Value)(Value[Key] aa) What are you trying to do with byPair? It's only defined for the MongoCursor: http://vibed.org/api/vibe.db.mongo.cursor/MongoCursor.byPair That std.array.byPair shows up means that you are using it for something else? source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function I guess you forgot a return here? Maybe it's easier to post your code again? I took this code from example: https://github.com/rejectedsoftware/vibe.d/blob/master/examples/mongodb/source/app.d And it is looking right now like below: import vibe.d; import std.algorithm, std.array; @path("/api") interface IFrontPageAPI { Json getHome(); } class FrontPageAPI : IFrontPageAPI { this(MongoCollection tmp) { collect = tmp; } private: MongoCollection collect; public: Json getHome() { logInfo("Getting HomePage from DB"); Bson query = Bson(["_id" : Bson("homepage")]); auto result = collect.find(query); logInfo("Iterating results..."); foreach (i, doc; result.byPair) logInfo("Item %d: %s", i, doc.toJson().toString()); logInfo("Sending Json"); return Json(result.map!(a => a.toJson).array); } } When i remove this piece of code: logInfo("Iterating results..."); foreach (i, doc; result.byPair) logInfo("Item %d: %s", i, doc.toJson().toString()); everything is working as expecting. One more question, what it does mean: return Json(result.map!(a => a.toJson).array); Why we cant just use something like result.toJson? When i
Re: VibeD - undefinded identifier
On Sunday, 23 July 2017 at 16:27:40 UTC, Mike Parker wrote: On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote: this(auto tmp) You need to specify a type here instead of using auto. Thanks for reply i fix it. But right now im getting: ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead. source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are: /usr/include/dlang/dmd/std/array.d(419,6): std.array.byPair(Key, Value)(Value[Key] aa) source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function
Re: VibeD - undefinded identifier
On Sunday, 23 July 2017 at 15:43:31 UTC, Seb wrote: On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote: Hello Im trying to create REST api for my simple web page. My code is looking like below: module service.frontpage; import vibe.d; @path("/api") interface IFrontPageAPI { Json getHome(); } class FrontPageAPI : IFrontPageAPI { this(auto tmp) { auto collect = tmp; } Json getHome() { logInfo("Getting HomePage from DB"); Bson query = Bson(["_id" : Bson("homepage")]); auto result = collect.find(query); logInfo("Iterating results..."); foreach (i, doc; result.byPair) logInfo("Item %d: %s", i, doc.toJson().toString()); return result.toJson(); } } But when i try to compile it im getting: source/service/frontpage.d(15,3): Error: undefined identifier 'tmp', did you mean alias 'cmp'? Did i miss something? You need to - use Class variables - use types instead auto (here Mongo collection) - return a proper Json Have a look at: https://is.gd/7AMsKs And play with it - no output means no complication error. Thank you it solves my problem, but now i'm getting: source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are: /usr/include/dlang/dmd/std/array.d(419,6): std.array.byPair(Key, Value)(Value[Key] aa) source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function What is strange it is just copy/paste from example: https://github.com/rejectedsoftware/vibe.d/blob/master/examples/mongodb/source/app.d
VibeD - undefinded identifier
Hello Im trying to create REST api for my simple web page. My code is looking like below: module service.frontpage; import vibe.d; @path("/api") interface IFrontPageAPI { Json getHome(); } class FrontPageAPI : IFrontPageAPI { this(auto tmp) { auto collect = tmp; } Json getHome() { logInfo("Getting HomePage from DB"); Bson query = Bson(["_id" : Bson("homepage")]); auto result = collect.find(query); logInfo("Iterating results..."); foreach (i, doc; result.byPair) logInfo("Item %d: %s", i, doc.toJson().toString()); return result.toJson(); } } But when i try to compile it im getting: source/service/frontpage.d(15,3): Error: undefined identifier 'tmp', did you mean alias 'cmp'? Did i miss something?
Re: Counting time difference.
On Wednesday, 3 February 2016 at 22:45:03 UTC, sigod wrote: On Wednesday, 3 February 2016 at 22:27:07 UTC, holo wrote: When i start same program on server in different timezone difference is much higher (more than hour). Why it is happening? Timezones shouldnt have influence on such equation. Try using `Clock.currTime(UTC())`. And make sure all instances produce timezone independent timestamps. Thank you, now i see that AWS is getting back Time in UTC so that solved problem. @Jonathan M Davis like i wrote above using UTC time helped me. Thank you for information about that monotonic clock will check it too for learning purpose. //holo
Counting time difference.
Hello I'm trying to count time difference to count time for how long instance is running. Here is how am i doing it: import std.stdio, sigv4, kxml.xml, awsxml; import std.datetime; import std.string; void main() { SigV4 req = new SigV4; IfResult result = req.go; AwsXml instancesData = new AwsXml(result.retResult); auto instsData = instancesData.getInstancesData; foreach(instData; instsData) { auto currentTime = Clock.currTime(); // auto currentStrTime = currentTime.toISOExtString().split(".")[0]; // auto currTime = SysTime.fromISOExtString(currentStrTime); auto launchStrTime = instData.instanceLaunchTime; auto launchTime = SysTime.fromISOExtString(launchStrTime); auto diff = currentTime - launchTime; auto zagadka = launchTime + diff; writeln(instData.instanceId ~ " " ~ instData.instanceName ~ " " ~ instData.instanceState ~ " " ~ instData.instanceLaunchTime ~ " " ~ instData.instanceReason); writeln(launchTime); writeln(currentTime); writeln(diff); writeln(zagadka); } } but the results do not match (launchTime + diff != currentTime) ... 2015-Mar-17 14:32:24Z 2016-Feb-03 23:21:41.2374511 46 weeks, 1 day, 7 hours, 49 minutes, 17 secs, 237 ms, 451 μs, and 1 hnsec 2016-Feb-03 22:21:41.2374511Z When i start same program on server in different timezone difference is much higher (more than hour). Why it is happening? Timezones shouldnt have influence on such equation. //holo
Re: kxml - parsing AWS API xml respond
On Tuesday, 20 October 2015 at 13:23:47 UTC, opticron wrote: I think part of the issue here is that holo isn't quite sure of what information [s]he needs out of the XML reply and how to do that with XPath. The first post mentioned getting instanceId and launchTime, so I'll start there using the AWS example XML found here: https://github.com/polopoly/cloud-ui/blob/master/aws/examples/DescribeInstances.xml You can find all parent nodes that provide this set of information with XPath query "//instancesSet/item". From there, you can iterate over the list and pull out the information you need: auto parents = root.parseXPath("//instancesSet/item"); foreach(parent; parents) { string instanceId = parent.parseXPath("instanceId")[0].getCData(); string launchTime = parent.parseXPath("launchTime")[0].getCData(); } In the process of checking this out, I discovered that subnode predicates are broken (attribute predicates work just fine, I need to write more unittests). I guess I have some things to work on tonight. Sorry i augment you work ;) . I was continuing with my project i came across next problem. When im checking instance name with such code: auto test = list.parseXPath(`//tagSet/item[key="Name"]/value`)[0].goCData; it is compiling properly but it is breaking program when is no name set. I make quick workaround: auto tmp = list.parseXPath(`//tagSet/item[key="Name"]/value`); if(tmp == null) { instances[tmpinst].instanceName = ""; } else { auto instances[tmpinst].instanceName = tmp[0].getCData; } but is there any reason why it is not taken into consideration in "getCData" method?
Re: kxml - parsing AWS API xml respond
I have no idea what i'm doing, but i did it (i just choose 1 element of array): void main() { string xmlstring = cast(string)read("test.xml"); XmlNode newdoc = xmlstring.readDocument(); XmlNode[] searchlist = newdoc.parseXPath(`//instancesSet/item`); foreach(list; searchlist) { string test1 = list.parseXPath(`//instanceId`)[0].getCData; writeln(test1); } } Is it really how it should looks like? Is there any better/more beautiful solution?
Re: kxml - parsing AWS API xml respond
On Monday, 19 October 2015 at 16:12:56 UTC, Kagamin wrote: If you don't want parent tag, then: XmlNode[] searchlist2 = newdoc.parseXPath("//launchTime"); I wanted parent tag, i think. I almost figure out what i needed, but getCData stops working :/ void main() { string xmlstring = cast(string)read("test.xml"); XmlNode newdoc = xmlstring.readDocument(); XmlNode[] searchlist = newdoc.parseXPath("//instancesSet/item"); foreach(list, searchlist) { string test = list.parseXPath("instanceId").getCData(); writeln(test); } } [holo@ultraxps test]$ dub Performing "debug" build using dmd for x86_64. kxml 1.0.0: target for configuration "library" is up to date. test ~master: building configuration "application"... source/app.d(20,57): Error: no property 'getCData' for type 'XmlNode[]' dmd failed with exit code 1. [holo@ultraxps test]$
Re: kxml - parsing AWS API xml respond
On Monday, 19 October 2015 at 12:54:52 UTC, Kagamin wrote: Select parent tags and get data from their child tags like instanceId. void main() { string xmlstring = cast(string)read("test.xml"); XmlNode newdoc = xmlstring.readDocument(); XmlNode[] searchlist = newdoc.parseXPath("//instanceId"); XmlNode[] searchlist2 = searchlist.parseXPath("//launchTime"); writeln(searchlist2); } Not working but probably its not what you thought, how to select it do you have some example?
Re: kxml - parsing AWS API xml respond
Sorry i was trying to use that XPaths to take out instanceId and laounchTime but i always get an empty respond. //instanceId/launchTime" - empty //instanceId/* - empty too //instanceId.launchTime - empty too How to take instance id and variable which im interest in or few/all for instanceId?
Re: kxml - parsing AWS API xml respond
On Monday, 19 October 2015 at 11:53:08 UTC, Kagamin wrote: On Monday, 19 October 2015 at 04:49:25 UTC, holo wrote: Why there is needed that "//" before tag? That's an XPath expression. Need, to read about that XPaths. How to drop tags from respond? I have such result of parsing by id tag: i-xx I need only value. Is there some equivalent to ".text" from std.xml? Try getCData https://github.com/opticron/kxml/blob/master/source/kxml/xml.d#L309 getCData is working like expected. Thank you for tips.
Re: kxml and dub package manager.
On Monday, 19 October 2015 at 06:20:19 UTC, Mike Parker wrote: On Monday, 19 October 2015 at 03:33:18 UTC, holo wrote: ok i fugure out it. When i do initiation i need to add dependencies (thought it is enough to add them to sdl file). Proper initiation should look like that: dub init projectname kxml No, you should never need to do that. I think your problem might be related to the fact that in your dub.sdl you named your project "kxml". You shouldn't name a project the same as one of your dependencies. Tried too with different name as i came to same idea and created new one with other name. It was behaving that same, so it is not cause of problem.
kxml - parsing AWS API xml respond
I'm trying to take out from AWS respond needed information. Here is cut off part of example respond: ... i-x ami-x 16 running ip-xxx.ec2.internal ec2-xx.compute-1.amazonaws.com ec2key 0 marketplace c3.large 2015-08-18T16:51:11.000Z ... With such code im getting only information about all instance ids: string xmlstring = cast(string)read("test.xml"); XmlNode newdoc = xmlstring.readDocument(); XmlNode[] searchlist = newdoc.parseXPath("//instanceId"); writeln(searchlist); What i need is that to take out from there instance id and eg LaunchTime for that instance. How can i do it using kxml? Why there is needed that "//" before tag? In other way im not getting any respond. What is interesting when i do that same with first tag appearing in respond it is not needed. From other hand with "///" i'm getting much more information but not separated by coma. How to drop tags from respond? I have such result of parsing by id tag: i-xx I need only value. Is there some equivalent to ".text" from std.xml?
Re: kxml and dub package manager.
ok i fugure out it. When i do initiation i need to add dependencies (thought it is enough to add them to sdl file). Proper initiation should look like that: dub init projectname kxml
Re: kxml and dub package manager.
On Monday, 19 October 2015 at 03:04:28 UTC, drug wrote: 19.10.2015 02:57, holo пишет: How to make dub to work for me? Try ``` import kxml.xml; // instead of import kxml; ``` Same: [holo@ultraxps kxml]$ dub run Performing "debug" build using dmd for x86_64. kxml ~master: building configuration "application"... source/app.d(1,8): Error: module xml is in file 'kxml/xml.d' which cannot be read import path[0] = source/ import path[1] = /usr/include/dlang/dmd dmd failed with exit code 1. [holo@ultraxps kxml]$
kxml and dub package manager.
I want to add xml support to my application so i fetched kxml library with dub but it don't want to work for me. Steps and test code: [holo@ultraxps kxml]$ cat dub.sdl name "kxml" description "A minimal D application." copyright "Copyright © 2015, holo" authors "holo" dependencies "kxml" version="1.0.0" [holo@ultraxps kxml]$ dub run Performing "debug" build using dmd for x86_64. kxml ~master: building configuration "application"... source/app.d(1,8): Error: module kxml is in file 'kxml.d' which cannot be read import path[0] = source/ import path[1] = /usr/include/dlang/dmd dmd failed with exit code 1. [holo@ultraxps kxml]$ cat source/app.d import kxml; import std.stdio; import std.file; struct instance { string id; string name; string type; string state; } void main() { } [holo@ultraxps kxml]$ How to make dub to work for me?
Re: Curl, how to recieve data.
On Sunday, 18 October 2015 at 21:11:58 UTC, sigod wrote: On Sunday, 18 October 2015 at 21:01:05 UTC, holo wrote: On Sunday, 18 October 2015 at 20:12:42 UTC, sigod wrote: [...] I changed it to such code: ... auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); client.onReceive = (ubyte[] data){receivedData.put(data); return data.length;}; client.perform(); return new Result(receivedData.data); ... auto receivedData = appender!string(); ... and it is really much more faster than it was - hope it is what you had on your mind. Yes, this is exactly what I meant. Thank you for help.
Re: Curl, how to recieve data.
On Sunday, 18 October 2015 at 20:12:42 UTC, sigod wrote: On Sunday, 18 October 2015 at 20:05:24 UTC, holo wrote: @sigod Actually im working on ec2 requests. Thank you for help, it is working right now. I don't know why i was trying "+=" before instead of "~=". Is it good solution to make it such way? Not really as it will trigger allocation on every call. Better use [`Appender`][0]. [0]: http://dlang.org/phobos/std_array.html#.Appender I changed it to such code: ... auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); client.onReceive = (ubyte[] data){receivedData.put(data); return data.length;}; client.perform(); return new Result(receivedData.data); ... auto receivedData = appender!string(); ... and it is really much more faster than it was - hope it is what you had on your mind.
Re: Curl, how to recieve data.
@sigod Actually im working on ec2 requests. Thank you for help, it is working right now. I don't know why i was trying "+=" before instead of "~=". Is it good solution to make it such way? @Suliaman I need to collect information about my instances and put it to DB. I want to present those datas with with vibe.d, but collector want to make without too many dependencies (i was thinking to make it as separate thread but simplest solution will be just to use cron to run collector i think)
Curl, how to recieve data.
I'm trying to receive data from curl request my sample code looks like that: ... auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); client.onReceive = (ubyte[] data) { recievedData = data; return data.length; }; client.perform(); return new Result(recievedData); ... ubyte[] receivedData; ... but im getting only last (like from "tail" command in unix systems) part of data which im expecting. How to receive and save whole data which came as feedback for request? Or is there some other way to access it after usage of client.perform method?
Re: Class, constructor and inherance.
I created interface IfRequestHandler it is used only by one class RequestHandlerXML right now but thanks to such solution i can create more classes with same interface which can handle it in different way.. eg second can be RequestHandlerCSVReport or RequestHandlerSendViaEmail. Is it this what you ware mentioning? Bellow working code: / //main.d: / #!/usr/bin/rdmd import std.stdio, sigv4, conf; void main() { ResultHandlerXML hand = new ResultHandlerXML; SigV4 req = new SigV4(hand); //req.ResultHandler = hand; req.go(); hand.processResult(); } / //conf.d: / module conf; import std.stdio, std.process; import std.net.curl:exit; interface IfConfig { void set(string val, string var); string get(string var); } class Config : IfConfig { this() { this.accKey = environment.get("AWS_ACCESS_KEY"); if(accKey is null) { writeln("accessKey not available"); exit(-1); } this.secKey = environment.get("AWS_SECRET_KEY"); if(secKey is null) { writeln("secretKey not available"); exit(-1); } } public: void set(string val, string var) { switch(var) { case "accKey": accKey = val; break; case "secKey": secKey = val; break; default: writeln("Can not be set, not such value"); } } string get(string var) { string str = ""; switch(var) { case "accKey": return accKey; case "secKey": return secKey; default: writeln("Can not be get, not such value"); } return str; } // private: string accKey; string secKey; } / //sigv4.d / module sigv4; import std.stdio, std.process; import std.digest.sha, std.digest.hmac; import std.string; import std.conv; import std.datetime; import std.net.curl; import conf; interface IfSigV4 { IfResultHandler go(ResultHandlerXML ResultHandler); } interface IfResultHandler { void setResult(int content); void processResult(); } class ResultHandlerXML : IfResultHandler { void setResult(int content) { this.xmlresult = content; } void processResult() { writeln(xmlresult); } private: int xmlresult; } class SigV4 : IfSigV4 { //could be changed to take some structure as parameter instead of such ammount of attributes this(string methodStr = "GET", string serviceStr = "ec2", string hostStr = "ec2.amazonaws.com", string regionStr = "us-east-1", string endpointStr = "https://ec2.amazonaws.com";, string payloadStr = "", string parmStr = "Action=DescribeInstances") in { writeln(parmStr); } body { conf.Config config = new conf.Config; this.method = methodStr; this.service = serviceStr; this.host = hostStr; this.region = regionStr; this.endpoint = endpointStr; this.payload = payloadStr; this.requestParameters = parmStr; this.accessKey = config.get("accKey"); if(accessKey is null) { writeln("accessKey not available"); exit(-1); } this.secretKey = config.get("secKey"); if(secretKey is null) { writeln("secretKey not available"); exit(-1); } } public: string method; string service; string host; string region; string endpoint; string payload; string requestParameters; IfResultHandler ResultHandler; IfResultHandler go(ResultHandlerXML ResultHandler) { //time need to be set when we are sending request not before auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; canonicalURI = "/"; canonicalQueryString = requestParameters ~ this.Version; canonicalHeadersString = "host:" ~ this.host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n"; signedHeaders = "host;x-amz-date"; auto canonicalRequest = getCanonicalRequest(canonicalURI, canonicalQueryStri
Re: Class, constructor and inherance.
Thank you for example. I asked about it programmers at work too - PHP guys - and they explained me how you are see usage of that interfaces in my code. They prepare for me some "skeleton" on which i will try to build my solution. Will be back if i will have some code.
Re: Class, constructor and inherance.
Please again, any example? I'm trying to figure out how it should be done but i don't have any base from which i can build some solution. #!/usr/bin/rdmd import std.stdio; interface RequestResult { int add (int x); } class B : RequestResult { int add(int x) { return ++x; } } class A { RequestResult go(int varA, int varB) { return add(varA + varB); } } void main() { B b = new B(); A a = new A(); int x = 12; int y = 15; RequestResult c = A.go(x, y); } It even don't want to compile, but that probably not what you ware thinking about. [holo@ultraxps workplace]$ dmd test.d test.d(24): Error: undefined identifier 'add' test.d(38): Error: need 'this' for 'go' of type 'RequestResult(int varA, int varB)' [holo@ultraxps workplace]$
Re: Class, constructor and inherance.
Just some ideas: interface RequestResult { ... } RequestResult go(string[string] requestParameters) Basically it is same what you wrote in one of first posts. Interface is for declaration of methods which need to be implemented in class. How in that case is it possible to return RequestResult which is basically xml form as function/s and one of it will be go itself? Do you have any link to such solution which i can take as example? I was looking for it but didn't find such solution. One idea interface RequestResult { string doSomethingWithXML(string xmlString); RequestResult go(); //or just not put here that go? } Class SigV4 : RequestResult { ... go() { string xml; ... return doSomethingWithXML(xml); } doSomethingWithXML(string xmlString) { ... } } but i still don't see how it could work like that.
Re: Class, constructor and inherance.
I want to ask you for advises what i could do with that class to make it looks more "pro"/elegant/build in "proper way". Probably there are lot of mistakes which all beginners are doing. eg.: Did i use interface correctly? You are reasonably close: credential sig = new sigv4(); Why are you creating sigv4 object with type credential? How in your opinion should look interface for such class? Although go is not really doing what I expect it to do. To me go should be dedicated to performing a request given what ever you need for just that request. The class sigv4 is your global state aka what doesn't change per request. To me what go returns is whatever is the common denominator for what you need from it is. Not sure if i correctly understood that. Do you propose to take out eg signing process from go function and let it only to make requests? Signature is depending on kind of request and accurate time when request is send. That is why i all that things put to it. The implementation such as sigv4 can be configured with as much detail as possible extra that you need. The method go can for instance do what ever it needs to, to for fill it's goal. From what I can see, you probably want go to take the payload as an argument. That way you can reuse an instance of sigv4. Which is the ultimate goal. In plan i wanted to change variables per request that why i left that public variables and created that arguments for constructor - to have possibility to change that values durring creation of object. On example, im expecting it will be behaving like that: sigv4 obj = new sigv4(); content = obj.go();//will get instances list obj.request_parameters = "Action=DescribeRegions&Version=2013-10-15" content2 = obj.go();//will get list of regions destroy(obj); so that go will get me back what i need per request not per one object live, or it wont work like that? It indeed should not. Also remember go is currently returning an integer. Not data from the request. I test it with such code: void main() { SigV4 sig = new SigV4(); writeln(sig.go); sig.requestParameters = "Action=DescribeRegions&Version=2013-10-15"; writeln(sig.go()); } Actually it is working, first it returned to me default request DescribeInstances and right after that request DescirbeRegions. I understand that, before i will start parsing it with xml lib i need to convert it to string. Should it be solved in other way? go method returns xml form (need to find out how force AWS to return JSON cos i saw xml module in dlang is deprecated) with structures depends on query - in default query that will be list of instances with all parameters. That xml file i want to parse in higher classes/functions (different class for different request). If you need to use std.xml use it. We don't appear to be replacing it any time soon. So here is the thing. You are tieing your usage of the API to SigV4 standard. This is bad bad bad. Any updates or changes to it, will cause problems. If you want to tie yourself to said standard, then you don't need OOP. How it could be solved in that case to not be tied to SigV4? If there will came some other request signing process it will need to be implement in that "go" function. Is there possibility to make it universal for all kind of signing processes? How it should be done in OOP (some example, pseudo code)? Or i missed what you are pointing to? Another thing, you may want to consider to use an interface as the return type of go. That way your implementation of it can have extra fields/methods which if you know that it is sigv4 you can cast to, to get access to, should you need it. It interesting what you wrote. Can you show me some example (pseudo code) how it can be implemented and how it could be used? It think that it is what you wrote in previous post: Credential sigv4 = new sigv4(); Remember im really beginner and some obvious things are not necessarily such for me. :) Also classes start with a capital letter, like Credential or SigV4. And D prefers camal casing aka requestParameters for variables/functions. Updated my code according to those directions. //holo
Re: Class, constructor and inherance.
On Tuesday, 13 October 2015 at 02:03:46 UTC, Rikki Cattermole wrote: On 13/10/15 5:56 AM, holo wrote: @Rikki: If you didn't need to make it easily changeable I would say not even bother with OOP at all. Basically that what i had was enough for me and on top of that i could build my app. It need to just periodically check for new instances and if they are started or stopped and count "up and running time" for billing purpose. But like i said i want to learn programming in D and basically OOP too so i want to make it "proper way". BTW: i think right now i understand what tuple is, but still don't know for what to duplicate struct functionalities :). Those Templates still don't understand but i hope that will came naturally with time and practice. eg.. they are very similar to normal functions but you can call it with types not only attributes.. strange ;) I red yours advises and try to create according to it my own first class. I moved time functions and variables to method "go" they need to be current as possible when im sending request, if wont authorization could not pass.. so i think they shouldn't be in constructor. I moved some other variables too, and created interface. From all that things came out such monster which is working and doing its job :) module sigawsv4; import std.stdio, std.process; import std.digest.sha, std.digest.hmac; import std.string; import std.conv; import std.datetime; import std.net.curl; interface credential { int go(); } class sigv4 : credential { //could be changed to take some structure as parameter instead of such ammount of attributes this(string methodStr = "GET", string serviceStr = "ec2", string hostStr = "ec2.amazonaws.com", string regionStr = "us-east-1", string endpointStr = "https://ec2.amazonaws.com";, string payloadStr = "", string parmStr = "Action=DescribeInstances&Version=2013-10-15") { this.method = methodStr; this.service = serviceStr; this.host = hostStr; this.region = regionStr; this.endpoint = endpointStr; this.payload = payloadStr; this.request_parameters = parmStr; this.accessKey = environment.get("AWS_ACCESS_KEY"); this.secretKey = environment.get("AWS_SECRET_KEY"); } public: string method; string service; string host; string region; string endpoint; string payload; string request_parameters; int go() { //time need to be set when we are sending request not before auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; canonicalURI = "/"; canonicalQueryString = request_parameters; canonicalHeadersString = "host:" ~ this.host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n"; signedHeaders = "host;x-amz-date"; auto canonicalRequest = getCanonicalRequest(canonicalURI, canonicalQueryString, canonicalHeadersString, signedHeaders); string credentialScope = curDateStr ~ "/" ~ region ~ "/" ~ service ~ "/" ~ "aws4_request"; string stringToSign = algorithm ~ "\n" ~ xamztime ~ "\n" ~ credentialScope ~ "\n" ~ sha256Of(canonicalRequest).toHexString.toLower; auto signingKey = getSignatureKey(secretKey, curDateStr, region, service); string signature = hmac!SHA256(stringToSign.representation, signingKey).toHexString.toLower; string authorizationHeader = algorithm ~ " " ~ "Credential=" ~ accessKey ~ "/" ~ credentialScope ~ ", " ~ "SignedHeaders=" ~ signedHeaders ~ ", " ~ "Signature=" ~ signature; auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); auto content = client.perform(); return content; } private: const algorithm = "AWS4-HMAC-SHA256"; string accessKey; string secretKey; string currentClock; string currentDate; string curDateStr; string currentTime; string curTimeStr; string xamztime; string canonicalURI; string canonicalQueryString; string canonicalHeadersString; string signedHeaders; alias sign = hmac!SHA256; auto getSignatureKey(string key, string dateStamp, string regionName, string serviceName) { auto kString = ("AWS4" ~ key).representation;
Re: Class, constructor and inherance.
@Rikki: If you didn't need to make it easily changeable I would say not even bother with OOP at all. Basically that what i had was enough for me and on top of that i could build my app. It need to just periodically check for new instances and if they are started or stopped and count "up and running time" for billing purpose. But like i said i want to learn programming in D and basically OOP too so i want to make it "proper way". BTW: i think right now i understand what tuple is, but still don't know for what to duplicate struct functionalities :). Those Templates still don't understand but i hope that will came naturally with time and practice. eg.. they are very similar to normal functions but you can call it with types not only attributes.. strange ;) I red yours advises and try to create according to it my own first class. I moved time functions and variables to method "go" they need to be current as possible when im sending request, if wont authorization could not pass.. so i think they shouldn't be in constructor. I moved some other variables too, and created interface. From all that things came out such monster which is working and doing its job :) module sigawsv4; import std.stdio, std.process; import std.digest.sha, std.digest.hmac; import std.string; import std.conv; import std.datetime; import std.net.curl; interface credential { int go(); } class sigv4 : credential { //could be changed to take some structure as parameter instead of such ammount of attributes this(string methodStr = "GET", string serviceStr = "ec2", string hostStr = "ec2.amazonaws.com", string regionStr = "us-east-1", string endpointStr = "https://ec2.amazonaws.com";, string payloadStr = "", string parmStr = "Action=DescribeInstances&Version=2013-10-15") { this.method = methodStr; this.service = serviceStr; this.host = hostStr; this.region = regionStr; this.endpoint = endpointStr; this.payload = payloadStr; this.request_parameters = parmStr; this.accessKey = environment.get("AWS_ACCESS_KEY"); this.secretKey = environment.get("AWS_SECRET_KEY"); } public: string method; string service; string host; string region; string endpoint; string payload; string request_parameters; int go() { //time need to be set when we are sending request not before auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; canonicalURI = "/"; canonicalQueryString = request_parameters; canonicalHeadersString = "host:" ~ this.host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n"; signedHeaders = "host;x-amz-date"; auto canonicalRequest = getCanonicalRequest(canonicalURI, canonicalQueryString, canonicalHeadersString, signedHeaders); string credentialScope = curDateStr ~ "/" ~ region ~ "/" ~ service ~ "/" ~ "aws4_request"; string stringToSign = algorithm ~ "\n" ~ xamztime ~ "\n" ~ credentialScope ~ "\n" ~ sha256Of(canonicalRequest).toHexString.toLower; auto signingKey = getSignatureKey(secretKey, curDateStr, region, service); string signature = hmac!SHA256(stringToSign.representation, signingKey).toHexString.toLower; string authorizationHeader = algorithm ~ " " ~ "Credential=" ~ accessKey ~ "/" ~ credentialScope ~ ", " ~ "SignedHeaders=" ~ signedHeaders ~ ", " ~ "Signature=" ~ signature; auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); auto content = client.perform(); return content; } private: const algorithm = "AWS4-HMAC-SHA256"; string accessKey; string secretKey; string currentClock; string currentDate; string curDateStr; string currentTime; string curTimeStr; string xamztime; string canonicalURI; string canonicalQueryString; string canonicalHeaders
Re: AWS API Dlang, hmac sha256 function.
Thank you for info, i changed my code to use that build in template and changed "cast(ubyte[]" to "xxx.representation" and it is still working: #!/usr/bin/rdmd -L-lcurl module sigawsv4; import std.stdio, std.process; import std.digest.sha, std.digest.hmac; import std.string; import std.conv; import std.datetime; import std.net.curl; void main() { auto accessKey = environment["AWS_ACCESS_KEY"]; auto secretKey = environment["AWS_SECRET_KEY"]; auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; string method = "GET"; string service = "ec2"; string host = "ec2.amazonaws.com"; string region = "us-east-1"; string endpoint = "https://ec2.amazonaws.com";; string request_parameters = "Action=DescribeInstances&Version=2013-10-15"; alias sign = hmac!SHA256; auto getSignatureKey(string key, string dateStamp, string regionName, string serviceName) { auto kString = ("AWS4" ~ key).representation; auto kDate = sign(dateStamp.representation, kString); auto kRegion = sign(regionName.representation, kDate); auto kService = sign(serviceName.representation, kRegion); auto kSigning = sign("aws4_request".representation, kService); return kSigning; } string canonicalURI = "/"; string canonicalQueryString = request_parameters; string canonicalHeadersString = "host:" ~ host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n"; string signedHeaders = "host;x-amz-date"; string payloadHash = sha256Of("").toHexString.toLower; string canonicalRequest = method ~ "\n" ~ canonicalURI ~ "\n" ~ canonicalQueryString ~ "\n" ~ canonicalHeadersString ~ "\n" ~ signedHeaders ~ "\n" ~ payloadHash; string algorithm = "AWS4-HMAC-SHA256"; string credentialScope = curDateStr ~ "/" ~ region ~ "/" ~ service ~ "/" ~ "aws4_request"; string stringToSign = algorithm ~ "\n" ~ xamztime ~ "\n" ~ credentialScope ~ "\n" ~ sha256Of(canonicalRequest).toHexString.toLower; auto signingKey = getSignatureKey(secretKey, curDateStr, region, service); string signature = hmac!SHA256(stringToSign.representation, signingKey).toHexString.toLower; string authorizationHeader = algorithm ~ " " ~ "Credential=" ~ accessKey ~ "/" ~ credentialScope ~ ", " ~ "SignedHeaders=" ~ signedHeaders ~ ", " ~ "Signature=" ~ signature; auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); auto content = client.perform(); writeln(content); }
Re: Class, constructor and inherance.
On Monday, 12 October 2015 at 03:29:12 UTC, Rikki Cattermole wrote: On 12/10/15 4:13 PM, holo wrote: By the looks, I'm guessing you do not have much experience when it comes to OOP. I think you are wanting something a bit closer to: import std.typecons : tuple, TypeTuple; interface Credential { string encode(); } class SigV4 : Credential { this() { } string encode() { } private: } TypeTuple!(string, string) AWSKeys() { import std.process; return tuple(environment.get("AWS_ACCESS_KEY"), environment.get("AWS_SECRET_KEY")); } Yes you guessed good, i don't have any experience with OOP (i have some experience with C - simple AVR projects and tons of bash scripts) this is my first such kind of language which I'm trying to learn. For beginning I'm trying to avoid advanced things like templates or from your example touples (Touples looks for me like some kind of array but every cell can be different type, am i right? Eg tuple from your example is that same what string[2] var?) i was reading about it but for now i can't understand what they are and for what they are useful. Templates are just compile time arguments :) Mostly used for types and constants. Simple concept, just don't get too scared off by what is possible with template if's. Tuples are pretty simple. It's basically just a struct. They are not arrays. But they do have a similar behavior. With opIndex overloading. You could for example use: struct AWSKeys { string access, secret; static AWSKeys get() { import std.process : environment; return AWSKeys(environment.get("AWS_ACCESS_KEY"), environment.get("AWS_SECRET_KEY")); } } Instead of that free function and tuples. I'm trying to learn on the examples and honestly i'm not understand how that TypeTuple is resolving my problem with default values for classes? It wasn't meant to, I got started rewriting the example code you gave, and ehh gave up after the basics of the class/interface. Second thing that interfaces, are they needed? Without it you can write same function just compilator wont be screaming for it lack. An interface basically says, this object adheres to these methods. When dealing with possibly changing authentication/communication protocols you would have the interface being how you get what to send, but the actual implementation being whatever you want to be using. Keep in mind, this class which handles creating the messages to the remote api's is not per message sent. It is a global communication mechanism. If you didn't need to make it easily changeable I would say not even bother with OOP at all. I would recommend coming on to #d on Freenode, we can help you better there. Thank you for explanation tomorrow (today) will back to it and read it carefully, will join IRC too. //almost 6:00AM here :)
Re: Class, constructor and inherance.
On Monday, 12 October 2015 at 02:30:43 UTC, Meta wrote: On Monday, 12 October 2015 at 02:14:35 UTC, holo wrote: class credential { auto accessKey = environment.get["AWS_ACCESS_KEY"]; auto secretKey = environment.get["AWS_SECRET_KEY"]; } class sigv4 : credential { private: const algorithm = "AWS4-HMAC-SHA256"; auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; } You should set these in the constructor rather than in the class body (the only one that's okay to intialize here is `algorithm`, as it's a const string). I rewrite it to something like this: module sigawsv4 import std.stdio, std.process; import std.digest.sha, std.digest.hmac; import std.string; import std.conv; import std.datetime; import std.net.curl; class credential { auto accessKey = environment.get["AWS_ACCESS_KEY"]; auto secretKey = environment.get["AWS_SECRET_KEY"]; } class sigv4 : credential { this(string URI = "/", string queryStr = "Action=DescribeInstances&Version=2013-10-15", string headerStr = "host:" ~ host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n", string headers = "host;x-amz-date") { auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; string canonicalURI = URI; string canonicalQueryString = queryStr; string canonicalHeaderString = headerStr; string signedHeaders = headers; } public: string method; string service; string host; string region; string endpoint; string request_parameters; string payload; private: const algorithm = "AWS4-HMAC-SHA256"; auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; auto hmac_sha256(ubyte[] key, ubyte[] msg) { auto hmac = hmac!SHA256(key); hmac.put(msg); auto digest = hmac.finish; return digest; } auto getSignatureKey(string key, string dateStamp, string regionName, string serviceName) { ubyte[] kString = cast(ubyte[])("AWS4" ~ key); auto kDate = sign(kString, cast(ubyte[])dateStamp); auto kRegion = sign(kDate, cast(ubyte[])regionName); auto kService = sign(kRegion, cast(ubyte[])serviceName); auto kSigning = sign(kService, cast(ubyte[])"aws4_request"); return kSigning; } auto getCanonicalRequest(string canonicalURI, string canonicalQueryString, string canonicalHeaderString, string signedHeaders) { string payloadHash = sha256Of("").toHexString.toLower; string canonicalRequest = method ~ "\n" ~ canonicalURI ~ "\n" ~ canonicalQueryString ~ "\n" ~ canonicalHeadersString ~ "\n" ~ signedHeaders ~ "\n" ~ payloadHash; } } Is right now that constructor ok? My question was too if now i will inherit that class, will that default values be in child class available?
Re: Class, constructor and inherance.
By the looks, I'm guessing you do not have much experience when it comes to OOP. I think you are wanting something a bit closer to: import std.typecons : tuple, TypeTuple; interface Credential { string encode(); } class SigV4 : Credential { this() { } string encode() { } private: } TypeTuple!(string, string) AWSKeys() { import std.process; return tuple(environment.get("AWS_ACCESS_KEY"), environment.get("AWS_SECRET_KEY")); } Yes you guessed good, i don't have any experience with OOP (i have some experience with C - simple AVR projects and tons of bash scripts) this is my first such kind of language which I'm trying to learn. For beginning I'm trying to avoid advanced things like templates or from your example touples (Touples looks for me like some kind of array but every cell can be different type, am i right? Eg tuple from your example is that same what string[2] var?) i was reading about it but for now i can't understand what they are and for what they are useful. I'm trying to learn on the examples and honestly i'm not understand how that TypeTuple is resolving my problem with default values for classes? Second thing that interfaces, are they needed? Without it you can write same function just compilator wont be screaming for it lack.
Class, constructor and inherance.
Hello I'm trying to write my first class. I want to use it as module and build anothers on top of it. I read that default functions attributes are not inherited. Is it that same for constructor? This is how my class (not finished) is looking right now: class credential { auto accessKey = environment.get["AWS_ACCESS_KEY"]; auto secretKey = environment.get["AWS_SECRET_KEY"]; } class sigv4 : credential { public: string method; string service; string host; string region; string endpoint; string request_parameters; string payload; private: const algorithm = "AWS4-HMAC-SHA256"; auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; auto hmac_sha256(ubyte[] key, ubyte[] msg) { auto hmac = hmac!SHA256(key); hmac.put(msg); auto digest = hmac.finish; return digest; } auto getSignatureKey(string key, string dateStamp, string regionName, string serviceName) { ubyte[] kString = cast(ubyte[])("AWS4" ~ key); auto kDate = sign(kString, cast(ubyte[])dateStamp); auto kRegion = sign(kDate, cast(ubyte[])regionName); auto kService = sign(kRegion, cast(ubyte[])serviceName); auto kSigning = sign(kService, cast(ubyte[])"aws4_request"); return kSigning; } auto getCanonicalRequest(string canonicalURI, string canonicalQueryString, string canonicalHeaderString, string signedHeaders) { string payloadHash = sha256Of("").toHexString.toLower; string canonicalRequest = method ~ "\n" ~ canonicalURI ~ "\n" ~ canonicalQueryString ~ "\n" ~ canonicalHeadersString ~ "\n" ~ signedHeaders ~ "\n" ~ payloadHash; } this() { } } I need to set all variables by defaults values from "public" and all needed by function "getCanonicalRequest". But if i will use that class to build another should i set defaults again? If yes is there some solution for it? //holo
Re: AWS API Dlang, hmac sha256 function.
After long fight with previous code i try to rewrite "one-to-one" python example from http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html (GET part) from begging to D with full success. Here is working code in clear D. Im using hmac function which is available from 2.069 of phobos). It is not too beautiful but like i said im beginner, here it is: #!/usr/bin/rdmd -L-lcurl module sigawsv4; import std.stdio, std.process; import std.digest.sha, std.digest.hmac; import std.string; import std.conv; import std.datetime; import std.net.curl; void main() { auto accessKey = environment["AWS_ACCESS_KEY"]; auto secretKey = environment["AWS_SECRET_KEY"]; auto currentClock = Clock.currTime(UTC()); auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; string method = "GET"; string service = "ec2"; string host = "ec2.amazonaws.com"; string region = "us-east-1"; string endpoint = "https://ec2.amazonaws.com";; string request_parameters = "Action=DescribeInstances&Version=2013-10-15"; auto hmac_sha256(ubyte[] key, ubyte[] msg) { auto hmac = hmac!SHA256(key); hmac.put(msg); auto digest = hmac.finish; return digest; } alias sign = hmac_sha256; auto getSignatureKey(string key, string dateStamp, string regionName, string serviceName) { ubyte[] kString = cast(ubyte[])("AWS4" ~ key); auto kDate = sign(kString, cast(ubyte[])dateStamp); auto kRegion = sign(kDate, cast(ubyte[])regionName); auto kService = sign(kRegion, cast(ubyte[])serviceName); auto kSigning = sign(kService, cast(ubyte[])"aws4_request"); return kSigning; } string canonicalURI = "/"; string canonicalQueryString = request_parameters; string canonicalHeadersString = "host:" ~ host ~ "\n" ~ "x-amz-date:" ~ xamztime ~ "\n"; string signedHeaders = "host;x-amz-date"; string payloadHash = sha256Of("").toHexString.toLower; string canonicalRequest = method ~ "\n" ~ canonicalURI ~ "\n" ~ canonicalQueryString ~ "\n" ~ canonicalHeadersString ~ "\n" ~ signedHeaders ~ "\n" ~ payloadHash; string algorithm = "AWS4-HMAC-SHA256"; string credentialScope = curDateStr ~ "/" ~ region ~ "/" ~ service ~ "/" ~ "aws4_request"; string stringToSign = algorithm ~ "\n" ~ xamztime ~ "\n" ~ credentialScope ~ "\n" ~ sha256Of(canonicalRequest).toHexString.toLower; auto signingKey = getSignatureKey(secretKey, curDateStr, region, service); string signature = hmac_sha256(signingKey, cast(ubyte[])stringToSign).toHexString.toLower; string authorizationHeader = algorithm ~ " " ~ "Credential=" ~ accessKey ~ "/" ~ credentialScope ~ ", " ~ "SignedHeaders=" ~ signedHeaders ~ ", " ~ "Signature=" ~ signature; auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString); client.method = HTTP.Method.get; client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("Authorization", authorizationHeader); auto content = client.perform(); writeln(content); } Now will try to "pack" it to some class and make it more usable and universal. I think that thread can be closed/solved (if something like this exist here) Thank you all for help. //holo
Re: AWS API Dlang, hmac sha256 function.
On Friday, 9 October 2015 at 16:30:26 UTC, holo wrote: OK i find out error, in addRequestHeader i was using ":" after header name what casing problem. I removed it and now im getting "unauthorized". Here is how it looks right now: HTTP/1.1 401 Unauthorized\r\n [Expert Info (Chat/Sequence): HTTP/1.1 401 Unauthorized\r\n] Request Version: HTTP/1.1 Status Code: 401 Response Phrase: Unauthorized Transfer-Encoding: chunked\r\n Date: Fri, 09 Oct 2015 16:22:47 GMT\r\n Server: AmazonEC2\r\n \r\n [HTTP response 1/2] [Next request in frame: 8371] HTTP chunked response Data chunk (254 octets) Chunk size: 254 octets Data (254 bytes) In data field i can read: "AWS was not able to validate provided access credentials" Is my signing process incorrect? Maybe i will put my present code again: #!/usr/bin/rdmd -L-lcurl import std.stdio; import std.string; import std.file; import std.datetime; import std.process; import std.digest.sha; import std.net.curl; import std.uri; import sigv4; auto zone = "us-east-1"; auto service = "ec2"; void main() { auto accKey = environment["AWS_ACCESS_KEY"]; auto secKey = environment["AWS_SECRET_KEY"]; auto currentClock = Clock.currTime; auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; string[string] empty; SignableRequest r; r.dateString = curDateStr; r.timeStringUTC = curTimeStr; r.region = zone; r.service = service; r.canonicalRequest = CanonicalRequest( "POST", "/", ["action" : "DescribeInstances", "version" : "2013-10-15"], // ["accept" : "*/*", ["content-type" : "application/x-www-form-urlencoded; charset=utf-8", "host" : service ~ ".amazonaws.com", "x-amz-date" : xamztime], cast(ubyte[])""); //cast(ubyte[])"Action=DescribeInstances&Version=2013-10-15"); auto qParm = canonicalQueryString(r.canonicalRequest.queryParameters); auto sigHead = canonicalHeaders(r.canonicalRequest.headers); auto sigStr = signableString(r); auto sigKey = signingKey(secKey, curDateStr, zone, service); auto signature = sign(sigKey, cast(ubyte[])sigStr).toHexString().toLower(); writeln(); writeln(qParm); writeln(); writeln(sigHead); writeln(); writeln(sigStr); writeln(); writeln(signature); writeln(); auto client = HTTP(); // client.clearRequestHeaders; client.addRequestHeader("content-type", "application/x-www-form-urlencoded; charset=utf-8"); client.addRequestHeader("host", service ~ ".amazonaws.com"); client.addRequestHeader("x-amz-date", xamztime); client.addRequestHeader("authorization", "AWS4-HMAC-SHA256" ~ " " ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~ service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~ "content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature); auto url = "ec2.amazonaws.com/?" ~ "Action=DescribeInstances&Version=2013-10-15"; auto urlenc = encode(url); writeln(url); auto content = get(urlenc, client); writeln(content); } Is my signing process correct?
Re: AWS API Dlang, hmac sha256 function.
OK i find out error, in addRequestHeader i was using ":" after header name what casing problem. I removed it and now im getting "unauthorized". Here is how it looks right now: HTTP/1.1 401 Unauthorized\r\n [Expert Info (Chat/Sequence): HTTP/1.1 401 Unauthorized\r\n] Request Version: HTTP/1.1 Status Code: 401 Response Phrase: Unauthorized Transfer-Encoding: chunked\r\n Date: Fri, 09 Oct 2015 16:22:47 GMT\r\n Server: AmazonEC2\r\n \r\n [HTTP response 1/2] [Next request in frame: 8371] HTTP chunked response Data chunk (254 octets) Chunk size: 254 octets Data (254 bytes) In data field i can read: "AWS was not able to validate provided access credentials" Is my signing process incorrect?
Re: AWS API Dlang, hmac sha256 function.
I doped HTTP packets with wireshark, maybe that will be useful: First recived: Hypertext Transfer Protocol HTTP/1.1 400 Bad Request\r\n [Expert Info (Chat/Sequence): HTTP/1.1 400 Bad Request\r\n] Request Version: HTTP/1.1 Status Code: 400 Response Phrase: Bad Request Transfer-Encoding: chunked\r\n Date: Fri, 09 Oct 2015 15:43:37 GMT\r\n Cneonction: close\r\n Server: AmazonEC2\r\n \r\n [HTTP response 1/2] [Next request in frame: 7739] HTTP chunked response Second sent: Hypertext Transfer Protocol GET /?Action=DescribeInstances&Version=2013-10-15 HTTP/1.1\r\n [Expert Info (Chat/Sequence): GET /?Action=DescribeInstances&Version=2013-10-15 HTTP/1.1\r\n] Request Method: GET Request URI: /?Action=DescribeInstances&Version=2013-10-15 Request Version: HTTP/1.1 Host:: ec2.amazonaws.com\r\n User-Agent: Phobos-std.net.curl/2.069 (libcurl/7.44.0)\r\n Accept: */*\r\n Content-Type:: application/x-www-form-urlencoded; charset=utf-8\r\n X-Amz-Date:: 20151009T174337Z\r\n Authorization:: AWS4-HMAC-SHA256 Credential=AKIAIHGPAJQ2PJ47YKKA/20151009T174337Z/us-east-1/ec2/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=3049eff7d6b5084c2fee71d10d5cffb17e0df1a9fe76963859c423f637d0496e\r\n \r\n [Full request URI: http://: ec2.amazonaws.com/?Action=DescribeInstances&Version=2013-10-15] [HTTP request 2/2] What is strange why im getting first that 400 Bad Request packet and after that is sent GET one?
Re: AWS API Dlang, hmac sha256 function.
I correct typo (thats whats happening when you trying to solve problem after night at 6:00 AM ).But still have same error. When use curl with same generated creditentials im getting "400". What is strange when im using same generated headers and signature with command line curl im getting "500". My headers right now: auto client = HTTP(); client.clearRequestHeaders; client.addRequestHeader("Content-Type:", "application/x-www-form-urlencoded; charset=utf-8"); client.addRequestHeader("Host:", service ~ ".amazonaws.com"); client.addRequestHeader("X-Amz-Date:", xamztime); client.addRequestHeader("Authorization:", "AWS4-HMAC-SHA256" ~ " " ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~ service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~ "content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature); auto url = service ~ ".amazonaws.com?" ~ "Action=DescribeInstances&Version=2013-10-15"; writeln(url); auto content = get(url, client); writeln(content); Did i set up it correctly?
Re: AWS API Dlang, hmac sha256 function.
I was fighting with it a little bit and after all i just leave original function which was in code, removed dependencies from vibe.d and finally tried to contact api. Here is my present code: #!/usr/bin/rdmd -L-lcurl import std.stdio; import std.string; import std.file; import std.datetime; import std.process; import std.digest.sha; import std.net.curl; import sigv4; auto zone = "us-east-1"; auto service = "ec2"; void main() { auto accKey = environment["AWS_ACCESS_KEY"]; auto secKey = environment["AWS_SECRET_KEY"]; auto currentClock = Clock.currTime; auto currentDate = cast(Date)currentClock; auto curDateStr = currentDate.toISOString; auto currentTime = cast(TimeOfDay)currentClock; auto curTimeStr = currentTime.toISOString; auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z"; string[string] empty; SignableRequest r; r.dateString = curDateStr; r.timeStringUTC = curTimeStr; r.region = zone; r.service = service; r.canonicalRequest = CanonicalRequest( "GET", "/", ["action" : "DescribeInstances", "version" : "2013-10-15"], ["content-type" : "application/x-www-form-urlencoded; charset=utf-8", "host" : service ~ ".amazonaws.com", "x-amz-date" : xamztime], cast(ubyte[])""); auto qParm = canonicalQueryString(r.canonicalRequest.queryParameters); auto sigHead = canonicalHeaders(r.canonicalRequest.headers); auto sigStr = signableString(r); auto sigKey = signingKey(secKey, curDateStr, zone, service); auto signature = sign(sigKey, cast(ubyte[])sigStr).toHexString().toLower(); writeln(); writeln(qParm); writeln(); writeln(sigHead); writeln(); writeln(sigStr); writeln(); writeln(signature); writeln(); auto client = HTTP(); client.clearRequestHeaders; client.addRequestHeader("content-type:", "application/x-www-form-urlencoded; charset=utf-8"); client.addRequestHeader("host:", service ~ ".amazonaws.com"); client.addRequestHeader("x-amz-date:", xamztime); client.addRequestHeader("Authoryzation:", "AWS4-HMAC-SHA256" ~ " " ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~ service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~ "content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature); auto url = service ~ ".amazonaws.com?" ~ "Action=DescribeInstances&Version=2013-10-15"; writeln(url); auto content = get(url, client); writeln(content); } Everything is compiling but im getting 400 (Bad Request): [root@ultraxps aws]# ./header.d action=DescribeRegions&version=2013-10-15 content-type: application/x-www-form-urlencoded; charset=utf-8 host: ec2.amazonaws.com x-amz-date: 20151009T053800Z AWS4-HMAC-SHA256 20151009T053800Z 20151009/us-east-1/ec2/aws4_request 888595748692147ceafafcae3941ec0d83ac42c97641e4d954d7447a00c56270 69b1e4c5212cc6b485569fdfb43f7dde94413b36c50393c55d4810ced47f167b ec2.amazonaws.com?Action=DescribeRegions&Version=2013-10-15 std.net.curl.CurlException@/usr/include/dlang/dmd/std/net/curl.d(824): HTTP request returned status code 400 (Bad Request) /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(pure @safe bool std.exception.enforce!(std.net.curl.CurlException, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x65) [0x5004bd] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl._basicHTTP!(char)._basicHTTP(const(char)[], const(void)[], std.net.curl.HTTP)+0x1a2) [0x4fc0ba] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl.get!(std.net.curl.HTTP, char).get(const(char)[], std.net.curl.HTTP)+0x6a) [0x4fbec2] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_Dmain+0x840) [0x4f8f98] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x533eab] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2b) [0x533e67] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_d_run_main+0x1d2) [0x533d86] /tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(main+0x12) [0x52cb62] /usr/lib/
Re: AWS API Dlang, hmac sha256 function.
Congrats on getting it working! @Rikki Thanks :) I was trying to write my own lib from beginning based on examples but after some time i resign from that idea (will back to it when i will have some more experience) and right now im trying to customize that one from link which yawniek paste: https://github.com/yannick/vibe-aws/blob/master/source/vibe/aws/sigv4.d I removed import from vibe.d library and copy/paste missed functions formEncode and filterURLEncode (BTW: what that "(R)" mean in it? filterURLEncode(R)(ref R dst, ..., ..) ). Next thing what i did was to replace hmac function to use hmac module from newest library. Now whole code looks like this: module sigv4; import std.array; import std.algorithm; import std.digest.sha; import std.range; import std.stdio; import std.string; import std.format; import std.digest.hmac; const algorithm = "AWS4-HMAC-SHA256"; void filterURLEncode(R)(ref R dst, string str, string allowed_chars = null, bool form_encoding = false) { while( str.length > 0 ) { switch(str[0]) { case ' ': if (form_encoding) { dst.put('+'); break; } goto default; case 'A': .. case 'Z': case 'a': .. case 'z': case '0': .. case '9': case '-': case '_': case '.': case '~': dst.put(str[0]); break; default: if (allowed_chars.canFind(str[0])) dst.put(str[0]); else formattedWrite(dst, "%%%02X", str[0]); } str = str[1 .. $]; } } string formEncode(string str, string allowed_chars = null) @safe { auto dst = appender!string(); dst.reserve(str.length); filterURLEncode(dst, str, allowed_chars, true); return dst.data; } struct CanonicalRequest { string method; string uri; string[string] queryParameters; string[string] headers; ubyte[] payload; } string canonicalQueryString(string[string] queryParameters) { alias encode = formEncode; string[string] encoded; foreach (p; queryParameters.keys()) { encoded[encode(p)] = encode(queryParameters[p]); } string[] keys = encoded.keys(); sort(keys); return keys.map!(k => k ~ "=" ~ encoded[k]).join("&"); } string canonicalHeaders(string[string] headers) { string[string] trimmed; foreach (h; headers.keys()) { trimmed[h.toLower().strip()] = headers[h].strip(); } string[] keys = trimmed.keys(); sort(keys); return keys.map!(k => k ~ ":" ~ trimmed[k] ~ "\n").join(""); } string signedHeaders(string[string] headers) { string[] keys = headers.keys().map!(k => k.toLower()).array(); sort(keys); return keys.join(";"); } string hash(T)(T payload) { auto hash = sha256Of(payload); return hash.toHexString().toLower(); } string makeCRSigV4(CanonicalRequest r) { auto cr = r.method.toUpper() ~ "\n" ~ (r.uri.empty ? "/" : r.uri) ~ "\n" ~ canonicalQueryString(r.queryParameters) ~ "\n" ~ canonicalHeaders(r.headers) ~ "\n" ~ signedHeaders(r.headers) ~ "\n" ~ hash(r.payload); return hash(cr); } unittest { string[string] empty; auto r = CanonicalRequest( "POST", "/", empty, ["content-type": "application/x-www-form-urlencoded; charset=utf-8", "host": "iam.amazonaws.com", "x-amz-date": "20110909T233600Z"], cast(ubyte[])"Action=ListUsers&Version=2010-05-08"); auto sig = makeCRSigV4(r); assert(sig == "3511de7e95d28ecd39e9513b642aee07e54f4941150d8df8bf94b328ef7e55e2"); } struct SignableRequest { string dateString; string timeStringUTC; string region; string service; CanonicalRequest canonicalRequest; } string signableString(SignableRequest r) { return algorithm ~ "\n" ~ r.dateString ~ "T" ~ r.timeStringUTC ~ "Z\n" ~ r.dateString ~ "/" ~ r.region ~ "/" ~ r.service ~ "/aws4_request\n" ~ makeCRSigV4(r.canonicalRequest); } unittest { string[string] empty; SignableRequest r; r.dateString = "20110909"; r.timeStringUTC = "233600"; r.region = "us-east-1"; r.service = "iam"; r.canonicalRequest = CanonicalRequest( "POST", "/", empty, ["content-type": "application/x-www-form-urlencoded; charset=utf-8", "host": "iam.amazonaws.com", "x-amz-date": "20110909T233600Z"], cast(ubyte[])"Action=ListUsers&Version=2010-05-08"); auto sampleString = algorithm ~ "\n" ~ "20110909T233600Z\n" ~
Re: AWS API Dlang, hmac sha256 function.
On Monday, 5 October 2015 at 21:00:38 UTC, Vladimir Panteleev wrote: On Monday, 5 October 2015 at 19:43:39 UTC, holo wrote: @Vladimir where can i check or do you know when next version of phobos will be available? You can use Digger to get the latest version of D: https://github.com/CyberShadow/Digger Thank you, now it is working as expected. Hope i wont miss upgrade in my system to uninstall digger version before. [holo@ultraxps workplace]$ cat ./hmac.d #!/usr/bin/rdmd import std.stdio; import std.digest.sha, std.digest.hmac; import std.string : representation; void main() { auto hmac = hmac!SHA256("secretkey".representation); hmac.put("texttohash".representation); auto digest = hmac.finish(); writeln(digest); } [holo@ultraxps workplace]$ ./hmac.d [28, 169, 142, 131, 133, 104, 149, 47, 205, 215, 20, 154, 170, 148, 84, 170, 252, 36, 10, 119, 18, 25, 10, 145, 183, 133, 135, 252, 26, 124, 215, 6] [holo@ultraxps workplace]$ dmd --version DMD64 D Compiler v2.069-devel-5e73c30 Copyright (c) 1999-2015 by Digital Mars written by Walter Bright [holo@ultraxps workplace]$ Now need try to write some code which will connect me to AWS api based on examples.
Re: AWS API Dlang, hmac sha256 function.
On Saturday, 3 October 2015 at 23:58:39 UTC, Rikki Cattermole wrote: On 04/10/15 2:31 AM, holo wrote: On Saturday, 3 October 2015 at 12:50:58 UTC, Rikki Cattermole wrote: On 04/10/15 1:49 AM, holo wrote: On Saturday, 3 October 2015 at 12:22:11 UTC, Rikki Cattermole wrote: On 04/10/15 1:09 AM, holo wrote: [...] By the looks of things the problem is with SHA256, I'm guessing it doesn't have its block size defined. https://github.com/D-Programming-Language/phobos/blob/master/std/digest/digest.d#L394 You'll also need to update sha.d as well. Otherwise it looks good. I updated sha.d and now im getting following error: $ ./app.d /usr/include/dlang/dmd/std/digest/sha.d(225): Error: pure function 'std.digest.sha.SHA!(512u, 160u).SHA.transform' cannot call impure function 'core.cpuid.ssse3' /usr/include/dlang/dmd/std/digest/digest.d(285): Error: template instance std.range.primitives.isOutputRange!(SHA!(512u, 160u), const(ubyte)[]) error instantiating /usr/include/dlang/dmd/std/digest/digest.d(851): instantiated from here: isDigest!(SHA!(512u, 160u)) /usr/include/dlang/dmd/std/digest/sha.d(1179):while looking for match for WrapperDigest!(SHA!(512u, 160u)) Failed: ["dmd", "-v", "-o-", "./app.d", "-I."] And one more file (ssse3.d) needs to be updated. You are going to be playing whack a mole I think for a while. I downloaded whole master branch phobos and tried to use it (changed path in dmd.conf), but there are missing much more files (i think that master branch is not ready yet). So i get back to my original library and tried to updated file you mention. But i can't find it (ssse3.d), its not appearing in my stable lib and even in that master-branch from zip file. Apologies, I didn't see this till after I got to bed. It's core.cpuid not ssse3.d. It will be in druntime, not Phobos. Sorry i was busy at weekend. Thank you all for answers. I will try to write some solution basing on yawniek example and will back if some troubles appears. @Vladimir where can i check or do you know when next version of phobos will be available? @Rikki i downloaded both master zip files for runtime and phobos librarys all dependencies looks are satisfied right now but still getting errors: [holo@ultraxps workplace]$ dmd hmac.d hmac.o: In function `_D3std6digest4hmac58__T4hmacTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4hmacFNaNbNiNfMAxhZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC': hmac.d:(.text._D3std6digest4hmac58__T4hmacTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4hmacFNaNbNiNfMAxhZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC+0x18): undefined reference to `_D3std6digest3sha20__T3SHAVki512Vki160Z3SHA6__initZ' hmac.o: In function `_D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC6__ctorMFNaNbNcNiNfMAxhZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC': hmac.d:(.text._D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC6__ctorMFNaNbNcNiNfMAxhZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC+0x1b): undefined reference to `_D3std6digest3sha20__T3SHAVki512Vki160Z3SHA5startMFNaNbNiNfZv' hmac.d:(.text._D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC6__ctorMFNaNbNcNiNfMAxhZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC+0x31): undefined reference to `_D3std6digest3sha20__T3SHAVki512Vki160Z3SHA3putMFNaNbNiNeMAxhXv' hmac.d:(.text._D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC6__ctorMFNaNbNcNiNfMAxhZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC+0x3e): undefined reference to `_D3std6digest3sha20__T3SHAVki512Vki160Z3SHA6finishMFNaNbNiNeZG20h' hmac.o: In function `_D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC5startMFNaNbNcNiNjNfZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC': hmac.d:(.text._D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC5startMFNaNbNcNiNjNfZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC+0x8d): undefined reference to `_D3std6digest3sha20__T3SHAVki512Vki160Z3SHA5startMFNaNbNiNfZv' hmac.d:(.text._D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC5startMFNaNbNcNiNjNfZS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC+0xa4): undefined reference to `_D3std6digest3sha20__T3SHAVki512Vki160Z3SHA3putMFNaNbNiNeMAxhXv' hmac.o: In function `_D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC3putMFNaNbNcNiNjNfxAhXS3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki160Z3SHAVmi512Z4HMAC': hmac.d:(.text._D3std6digest4hmac58__T4HMACTS3std6digest3sha20__T3SHAVki512Vki1
Re: AWS API Dlang, hmac sha256 function.
On Saturday, 3 October 2015 at 12:50:58 UTC, Rikki Cattermole wrote: On 04/10/15 1:49 AM, holo wrote: On Saturday, 3 October 2015 at 12:22:11 UTC, Rikki Cattermole wrote: On 04/10/15 1:09 AM, holo wrote: [...] By the looks of things the problem is with SHA256, I'm guessing it doesn't have its block size defined. https://github.com/D-Programming-Language/phobos/blob/master/std/digest/digest.d#L394 You'll also need to update sha.d as well. Otherwise it looks good. I updated sha.d and now im getting following error: $ ./app.d /usr/include/dlang/dmd/std/digest/sha.d(225): Error: pure function 'std.digest.sha.SHA!(512u, 160u).SHA.transform' cannot call impure function 'core.cpuid.ssse3' /usr/include/dlang/dmd/std/digest/digest.d(285): Error: template instance std.range.primitives.isOutputRange!(SHA!(512u, 160u), const(ubyte)[]) error instantiating /usr/include/dlang/dmd/std/digest/digest.d(851): instantiated from here: isDigest!(SHA!(512u, 160u)) /usr/include/dlang/dmd/std/digest/sha.d(1179):while looking for match for WrapperDigest!(SHA!(512u, 160u)) Failed: ["dmd", "-v", "-o-", "./app.d", "-I."] And one more file (ssse3.d) needs to be updated. You are going to be playing whack a mole I think for a while. I downloaded whole master branch phobos and tried to use it (changed path in dmd.conf), but there are missing much more files (i think that master branch is not ready yet). So i get back to my original library and tried to updated file you mention. But i can't find it (ssse3.d), its not appearing in my stable lib and even in that master-branch from zip file.
Re: AWS API Dlang, hmac sha256 function.
On Saturday, 3 October 2015 at 12:22:11 UTC, Rikki Cattermole wrote: On 04/10/15 1:09 AM, holo wrote: On Saturday, 3 October 2015 at 05:02:58 UTC, Rikki Cattermole wrote: On 03/10/15 6:01 PM, Rikki Cattermole wrote: On 03/10/15 4:54 PM, holo wrote: [...] By the looks of that error message with paths, botan isn't actually added as a dependency. Can you please paste the dub file? O wait nevermind. Use dub to compile/run your program. You are using the shebang line! $ dub run [...] I can't say for certain if it is not exact. The reason I did not answer it is because I am not familiar with environment or any of what you are doing. I can unfortunately only point you in the right direction. Thank you for explaining how to use dub. I find out in master branch of phobos on git hub is included hmac.d module which will be added in next release of phobos so i think that will be best solution for me to stick with standard library for beginning. https://github.com/D-Programming-Language/phobos/tree/master/std I downloaded hmac.d and new version of digest.d file and place them in my phobos library. I think it exactly contains what i need but when im trying to test it i have such error: $ ./app.d ./app.d(9): Error: template std.digest.hmac.hmac cannot deduce function from argument types !(SHA!(512, 256))(immutable(ubyte)[]), candidates are: /usr/include/dlang/dmd/std/digest/hmac.d(202): std.digest.hmac.hmac(H) if (isDigest!H && hasBlockSize!H) /usr/include/dlang/dmd/std/digest/hmac.d(208): std.digest.hmac.hmac(H, ulong blockSize)(scope const(ubyte)[] secret) if (isDigest!H) Failed: ["dmd", "-v", "-o-", "./app.d", "-I."] My test code (based on unint test from module) looks like that: #!/usr/bin/rdmd import std.stdio; import std.digest.sha, std.digest.hmac; import std.string : representation; void main() { auto hmac = hmac!SHA256("secretkey".representation); auto digest = hmac.put("texttohash".representation).finish; writeln(digest); } Am i using it correctly? By the looks of things the problem is with SHA256, I'm guessing it doesn't have its block size defined. https://github.com/D-Programming-Language/phobos/blob/master/std/digest/digest.d#L394 You'll also need to update sha.d as well. Otherwise it looks good. I updated sha.d and now im getting following error: $ ./app.d /usr/include/dlang/dmd/std/digest/sha.d(225): Error: pure function 'std.digest.sha.SHA!(512u, 160u).SHA.transform' cannot call impure function 'core.cpuid.ssse3' /usr/include/dlang/dmd/std/digest/digest.d(285): Error: template instance std.range.primitives.isOutputRange!(SHA!(512u, 160u), const(ubyte)[]) error instantiating /usr/include/dlang/dmd/std/digest/digest.d(851): instantiated from here: isDigest!(SHA!(512u, 160u)) /usr/include/dlang/dmd/std/digest/sha.d(1179):while looking for match for WrapperDigest!(SHA!(512u, 160u)) Failed: ["dmd", "-v", "-o-", "./app.d", "-I."]
Re: AWS API Dlang, hmac sha256 function.
On Saturday, 3 October 2015 at 05:02:58 UTC, Rikki Cattermole wrote: On 03/10/15 6:01 PM, Rikki Cattermole wrote: On 03/10/15 4:54 PM, holo wrote: On Saturday, 3 October 2015 at 03:15:21 UTC, Rikki Cattermole wrote: You could implement it yourself, (it looks pretty easy). Or go the route of Botan: https://github.com/etcimon/botan/blob/3bdc835d5b9bad7523deaa86fe98b1fbb2f09d6b/source/botan/mac/hmac.d Thank you for answer. I used dub to fetch botan, after that i created project: dub init projectname botan and tried to add botan module: $ cat app.d #!/usr/bin/rdmd import std.stdio; import botan.mac.hmac; void main() { writeln("Edit source/app.d to start your project."); } but when im trying to run my simple app i get: $ ./app.d ./app.d(4): Error: module hmac is in file 'botan/mac/hmac.d' which cannot be read import path[0] = . import path[1] = /usr/include/dlang/dmd Failed: ["dmd", "-v", "-o-", "./app.d", "-I."] What am i doing wrong? How to add library with dub package manager? Im really beginner in D (basically in programming) sorry if im asking obvious questions. By the looks of that error message with paths, botan isn't actually added as a dependency. Can you please paste the dub file? O wait nevermind. Use dub to compile/run your program. You are using the shebang line! $ dub run According to my other question are that functions equivalent to that python one? If they are not doing exact that same i could not create auth string which is needed to connect to AWS api. I can't say for certain if it is not exact. The reason I did not answer it is because I am not familiar with environment or any of what you are doing. I can unfortunately only point you in the right direction. Thank you for explaining how to use dub. I find out in master branch of phobos on git hub is included hmac.d module which will be added in next release of phobos so i think that will be best solution for me to stick with standard library for beginning. https://github.com/D-Programming-Language/phobos/tree/master/std I downloaded hmac.d and new version of digest.d file and place them in my phobos library. I think it exactly contains what i need but when im trying to test it i have such error: $ ./app.d ./app.d(9): Error: template std.digest.hmac.hmac cannot deduce function from argument types !(SHA!(512, 256))(immutable(ubyte)[]), candidates are: /usr/include/dlang/dmd/std/digest/hmac.d(202): std.digest.hmac.hmac(H) if (isDigest!H && hasBlockSize!H) /usr/include/dlang/dmd/std/digest/hmac.d(208): std.digest.hmac.hmac(H, ulong blockSize)(scope const(ubyte)[] secret) if (isDigest!H) Failed: ["dmd", "-v", "-o-", "./app.d", "-I."] My test code (based on unint test from module) looks like that: #!/usr/bin/rdmd import std.stdio; import std.digest.sha, std.digest.hmac; import std.string : representation; void main() { auto hmac = hmac!SHA256("secretkey".representation); auto digest = hmac.put("texttohash".representation).finish; writeln(digest); } Am i using it correctly?
Re: AWS API Dlang, hmac sha256 function.
On Saturday, 3 October 2015 at 03:15:21 UTC, Rikki Cattermole wrote: You could implement it yourself, (it looks pretty easy). Or go the route of Botan: https://github.com/etcimon/botan/blob/3bdc835d5b9bad7523deaa86fe98b1fbb2f09d6b/source/botan/mac/hmac.d Thank you for answer. I used dub to fetch botan, after that i created project: dub init projectname botan and tried to add botan module: $ cat app.d #!/usr/bin/rdmd import std.stdio; import botan.mac.hmac; void main() { writeln("Edit source/app.d to start your project."); } but when im trying to run my simple app i get: $ ./app.d ./app.d(4): Error: module hmac is in file 'botan/mac/hmac.d' which cannot be read import path[0] = . import path[1] = /usr/include/dlang/dmd Failed: ["dmd", "-v", "-o-", "./app.d", "-I."] What am i doing wrong? How to add library with dub package manager? Im really beginner in D (basically in programming) sorry if im asking obvious questions. According to my other question are that functions equivalent to that python one? If they are not doing exact that same i could not create auth string which is needed to connect to AWS api.
AWS API Dlang, hmac sha256 function.
Hello I'm trying to contact AWS API with D according to documentation: http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html there is written example in python which i want to rewrite to D: http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html I want to ask about such 2 things: Is it equivalent in D: hexDigest!SHA256("sometexttohash"); for such python method: hashlib.sha256('sometexttohash').hexdigest() ? Maybe is there some prettier solution in D eg: toHexString(sha256Of("sometexttohash")); or SHA256 sha; sha.start(); string data = "sometexttohash"; sha.put(data); toHexString(sha.finish()) If those are equivalents to python method which should i choose? Last but not least, how to write such function in D: def sign(key, msg): return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest() ? I can't find in standard libraryt hmac function, is it existing? //holo
Re: Server side command execution.
On Monday, 28 September 2015 at 13:01:25 UTC, Adam D. Ruppe wrote: On Monday, 28 September 2015 at 11:44:32 UTC, holo wrote: if(to!string(buffer[0..received]) == "exit\n") You shouldn't need that to!string by the way. I believe that will work just comparing the buffer directly. Converting to string is more important when you are storing a copy than just comparing it. Yep it it like you wrote. Thank you for advice.
Re: Server side command execution.
On Monday, 28 September 2015 at 10:52:07 UTC, anonymous wrote: On Monday 28 September 2015 12:40, anonymous wrote: The client probably sends a newline; i.e. buffer[0 .. received] is "exit\n". Or more likely it's "exit\r\n". I changed condition to: if(to!string(buffer[0..received]) == "exit\n") { break; } and now it is working. Thank you all for help.
Re: Server side command execution.
On Monday, 28 September 2015 at 04:55:30 UTC, tcak wrote: On Sunday, 27 September 2015 at 23:56:10 UTC, holo wrote: Hello Im trying to execute commands on server side. Here is my server based on other example from forum: [...] You are comparing whole buffer to "exit" I changed my condition to: if(to!string(buffer[0..received]) == "exit") { break; } But it still dint help.
Server side command execution.
Hello Im trying to execute commands on server side. Here is my server based on other example from forum: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.algorithm; import std.conv; void main() { Socket server = new TcpSocket(); server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); server.bind(new InternetAddress(8080)); server.listen(1); Socket client = server.accept(); while(true) { char[1024] buffer = 0; auto received = client.receive(buffer); writefln("The client said:\n%s", buffer[0.. received]); enum header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n"; string response = header ~ "Hello World!\n"; client.send(response); if(to!string(buffer) == "exit") { break; } } client.shutdown(SocketShutdown.BOTH); client.close(); } When i send "exit" nothing is happening. How it should be done to make it working? //holo
Re: Client socket sending data only one time.
On Friday, 25 September 2015 at 04:38:06 UTC, tcak wrote: On Thursday, 24 September 2015 at 14:20:39 UTC, holo wrote: Hello I'm trying to connect to server and send data, with such simple client: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.socketstream; import std.process; import std.conv; import core.time; void main() { char[1024] buffer = 0; Socket client = new TcpSocket(); auto addrServer = new InternetAddress("localhost", 8080); client.connect(addrServer); while(1) { client.send(readln()); client.receive(buffer); writeln(buffer); buffer = 0; } } It is working but only one time, when I'm trying to send again (second loop of while) it's stooping on readln() but not sending input data. What am i missing here? Thanks in advance for any help. Where is the other side of this client? There must be a TCP Server to listen connections, and create a second TCPSocket for communication. There is only one TCPSocket in your code. A lot of things are missing there. I would suggest you to check "C TCPSocket example" in your favourite search engine. Sorry i forgot to paste server side. After your post i look on that side of my simple socket program and i find out the problem. This is example from other forum topic: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.algorithm; import std.conv; void main() { Socket server = new TcpSocket(); server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); server.bind(new InternetAddress(8080)); server.listen(1); while(true) { Socket client = server.accept(); char[1024] buffer = 0; auto received = client.receive(buffer); writefln("The client said:\n%s", buffer[0.. received]); enum header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n"; string response = header ~ "Hello World!\n"; client.send(response); client.shutdown(SocketShutdown.BOTH); client.close(); } } Lie we see after each data recive server is closing connection. I rewrite it to something like it: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.algorithm; import std.conv; void main() { Socket server = new TcpSocket(); server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); server.bind(new InternetAddress(8080)); server.listen(1); Socket client = server.accept(); while(true) { char[1024] buffer = 0; auto received = client.receive(buffer); writefln("The client said:\n%s", buffer[0.. received]); enum header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n"; string response = header ~ "Hello World!\n"; client.send(response); auto command = to!string(buffer[0.. received]); if(command == "exit") { break; } } client.shutdown(SocketShutdown.BOTH); client.close(); } Now everything is working like i needed.
Client socket sending data only one time.
Hello I'm trying to connect to server and send data, with such simple client: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.socketstream; import std.process; import std.conv; import core.time; void main() { char[1024] buffer = 0; Socket client = new TcpSocket(); auto addrServer = new InternetAddress("localhost", 8080); client.connect(addrServer); while(1) { client.send(readln()); client.receive(buffer); writeln(buffer); buffer = 0; } } It is working but only one time, when I'm trying to send again (second loop of while) it's stooping on readln() but not sending input data. What am i missing here? Thanks in advance for any help.