www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - remote execute program

reply Cecil Ward <d cecilward.com> writes:
I am wanting to write a short program (on a ‘server’ you could 
say) that takes a command, runs it (as on the command line, so an 
executable with arguments or a shell command) and returns a 
3-tuple with an int for the return code, plus the textual outputs 
that it generates to stdout and stderr. I can see a number of 
suitable routines in the D runtime libraries, which are already 
D-ified to save me a some trouble mindlessly converting code from 
C.

Where I could do with some help is as follows: I'm needing to 
send the commands to a remote box using http has to be used 
because the local-end program (on an iPad) that I have to 
interfacing to can only speak http/https, and can not handle just 
a straight eg TCP connection. Despite the overheads from using 
http, if I can employ gzip compression on the link then that will 
be a big gain.

Could anyone give me some general pointers for where to look?

The server box is a linux machine. I'm a very experienced 
professional C programmer but amazingly have never done anything 
with *nix in general or http-related C libraries.


I asked a question in this forum earlier about general low-level 
networking, but now this requirement has come up that mandates 
the use of very simple http and needs only synchronous 
operations. The impressive framework that is vibe.d has already 
been mentioned, but the amount of reading matter is rather 
daunting.

A simple D example of an http transaction would be very helpful.
Mar 22 2018
next sibling parent reply Kagamin <spam here.lot> writes:
You can just read the whole request into a buffer and parse it 
there.
Mar 23 2018
parent Cecil Ward <d cecilward.com> writes:
On Friday, 23 March 2018 at 07:57:33 UTC, Kagamin wrote:
 You can just read the whole request into a buffer and parse it 
 there.
Agreed.
Mar 23 2018
prev sibling next sibling parent Kagamin <spam here.lot> writes:
Or this: 
https://github.com/nextcardgame/lighttp/blob/master/examples/chat.d
Mar 23 2018
prev sibling parent reply Cecil Ward <d cecilward.com> writes:
On Friday, 23 March 2018 at 01:23:56 UTC, Cecil Ward wrote:
 I am wanting to write a short program (on a ‘server’ you could 
 say) that takes a command, runs it (as on the command line, so 
 an executable with arguments or a shell command) and returns a 
 3-tuple with an int for the return code, plus the textual 
 outputs that it generates to stdout and stderr. I can see a 
 number of suitable routines in the D runtime libraries, which 
 are already D-ified to save me a some trouble mindlessly 
 converting code from C.

 Where I could do with some help is as follows: I'm needing to 
 send the commands to a remote box using http has to be used 
 because the local-end program (on an iPad) that I have to 
 interfacing to can only speak http/https, and can not handle 
 just a straight eg TCP connection. Despite the overheads from 
 using http, if I can employ gzip compression on the link then 
 that will be a big gain.

 Could anyone give me some general pointers for where to look?

 The server box is a linux machine. I'm a very experienced 
 professional C programmer but amazingly have never done 
 anything with *nix in general or http-related C libraries.


 I asked a question in this forum earlier about general 
 low-level networking, but now this requirement has come up that 
 mandates the use of very simple http and needs only synchronous 
 operations. The impressive framework that is vibe.d has already 
 been mentioned, but the amount of reading matter is rather 
 daunting.

 A simple D example of an http transaction would be very helpful.
It's not really a D question, in a sense, it's just that I am out of my depth. And wrapping fat C libraries, particularly converting .h files is something that I don't have any experience of, so something ready-cooked in D would b good. What library routines are available - or do I have to start wading through the vastness of vibe.d?
Mar 23 2018
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/23/2018 12:25 PM, Cecil Ward wrote:
 On Friday, 23 March 2018 at 01:23:56 UTC, Cecil Ward wrote:
 I am wanting to write a short program (on a ‘server’ you could say)
 that takes a command, runs it (as on the command line, so an
 executable with arguments or a shell command) and returns a 3-tuple
 with an int for the return code, plus the textual outputs that it
 generates to stdout and stderr.
A popular way of doing it is using a REST API. Your http server should direct certain URLs to your service and your service should return the results back. The messages are typically passed as JSON objects. Another method is using grpc but D is not among the official languages at their site.
 The
 impressive framework that is vibe.d has already been mentioned, but
 the amount of reading matter is rather daunting.
If you don't want to use vibe.d I'm sure Adam D. Ruppe's arsd repo has useful modules for you: https://github.com/adamdruppe Regarding vibe.d, I hope others can show you simple examples but a REST service was trivial when I played with vibe.d a few years ago. Look at "Example of a simple HTTP server" at their site: import vibe.vibe; void main() { listenHTTP(":8080", &handleRequest); runApplication(); } void handleRequest(HTTPServerRequest req, HTTPServerResponse res) { if (req.path == "/") res.writeBody("Hello, World!"); } Just implement handleRequest and you're done. But it can be better if your service is RESTful. You can find tutorials here: http://vibed.org/tutorials A fortunate problem is that vibe.d has been easier to use since it started allowing main() written by you. (In the past, it had main() calling your function.) Ali
Mar 23 2018