Thread overview
My simple internet client made in Dlang.
Feb 03, 2021
Marcone
Feb 04, 2021
Ali Çehreli
Feb 04, 2021
Max Haughton
February 03, 2021
I study Dlang for just over a year only and have already risked creating a program even with a graphical interface and distribute it on the internet. Here is a part of that program, just the code without a graphical interface. It is a program for modifying http headers. You connect your program to the local port of this script, this script plays the role of a server to receive your program. Receive the HTTP request from the program, connect to the remote server playing a client role, modify the HTTP request from the program and connect to the remote server.


I would like the completely relevant opinion of you my programmer friends in Dlang if my code is good or if it is very noob.

My Code: https://github.com/cerejavermelha/Dlang-Client-Server-Socket-Dropbear-Injector/blob/master/Dlang_Client_Server_Socket_Dropbear_Injector.d
February 04, 2021
On 2/3/21 8:44 AM, Marcone wrote:
> relevant opinion

I think the following would be improvements:

>    char[8192] request;

I don't know the protocol but obviously 8192 must be sufficient.

>    auto rq = c.receive(request);

So, what is actually received is request[0..rq]. I would call the first one 'buffer' and the actual part 'request':

  char[8192] buffer;
  const length = c.receive(buffer);
  enforce (length != Socket.ERROR, "Failed to read from socket.");
  auto request = buffer[0 .. length];

Each to!string is an extra copy below (and more in your code):

>    string host = to!string(request)[to!string(request).indexOf("CONNECT")+7..to!string(request).indexOf(":")].strip();

I would make a variable first (you can find better variable names :) ):

  string str = to!string(request);
  const conn = str.indexOf("CONNECT") + 7;
  const colon = str.indexOf(":");
  const http = str.indexOf("HTTP/");

  string host = str[conn .. colon].strip();
  string port = str[colon + 1 .. http].strip();

Even though performance of multiple to!string calls is not detectable in this program that interacts with sockets, I still think reducing the calls to to!string helps with readability and is less error-prone. :)

Ali
February 04, 2021
On Thursday, 4 February 2021 at 20:54:15 UTC, Ali Çehreli wrote:
> On 2/3/21 8:44 AM, Marcone wrote:
>> [...]
>
> I think the following would be improvements:
>
> >    [...]
>
> I don't know the protocol but obviously 8192 must be sufficient.
>
> [...]

Less calls to std.conv can also mean less surface area for exceptions to throw from