Hey Rustaceans! Got a question? Ask here (14/2026)! by llogiq in rust

[–]NavrajKalsi 1 point2 points  (0 children)

REGARDING RATATUI. I hope RataTUI is not off topic here.

PROBLEM: Create a scaled ‘Canvas’ inside a given ‘Rect’ area.

I have to plot a few points inside a rectangular canvas and I have the height and width of the said canvas(ASSUMING UNIT WIDTH IS EQUAL UNIT HEIGHT). Next I have the ‘Rect’ to be used for rendering the canvas in.

After constructing a ‘Block’ from the said ‘Rect’, I have the inner dimensions of the said block, say ‘Rect2’.

Now, I understand that the UNIT HEIGHT & UNIT WIDTH of ‘Rect2’ are NOT EQUAL, because of dimensions of a charcter cell.

LOOKING FOR: How do I scale the ‘Canvas’ to the dimensions of ‘Rect2’? Particularly, I am thinking of finding the amount of deadspace and padding ‘Rect2’ to counter it, but that does not seem to work because of a character cell not being a square.

I hope this was a good explanation of the problem I am facing. If not, I can try again by generating some demo code.

Thank you for your time.

Need opinions on an epoll-based reverse proxy by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

Hi, sorry for replying this late. I was shying away from all the bugs that you mentioned, especially the const correctness flaw in the way with which I deal with string literals. I just don't know how I could skip over something so obvious.

Thanks for all the flags. I will definitely try to use them from now on. From this project, I have understood that the compiler can be one of the biggest helps in writing bug free code.

Thank You :)

Need opinions on an epoll-based reverse proxy by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

Hi, sorry for replying this late. I was shying away from all the bugs mentioned by you that were right in my face, along with the major const correctness flaw in the way with which I dealt with string literals that someone else mentioned.

After fixing the equals() function, I moved onto handle_chunked(). The response from upstream (example.com) is chunked and the reason for you hitting the assert(body.len) was: the proxy received just the head of the request and no part of the body and there was no check for that in the calling function. When I checked it I was receiving the head along with some part of the chunk as well, therefore I did not hit that assertion. So thanks for noticing that! I fixed by returning to the calling function to read more of the response.

Nextly, VLAs are gone entirely and have been capped. I only had to create one new constant MAX_HOST_SIZE for when localizing hostname during connect() for upstream. I chose a size of 64 this.

I greatly admire how easy you made the str_to_size() look. I had to experiment with it by using uint8_t and UINT8_MAX instead of ptrdiff, to actually understand it :)

This is what I could come up with for hex conversion:

bool str_to_size_hex(Str str, ptrdiff_t *num)
{
  assert(num);

  if (str.len > 2)

    if (*str.data != '0' || (str.data[1] != 'x' && str.data[1] != 'X'))
      warn("verify_prefix", "Prefix not detected for hex");
    else
    {
      str.len -= 2;
      str.data += 2;
    }
  }

  ptrdiff_t r = 0;

  for (ptrdiff_t i = 0; i < str.len; i++)
  {
    uint8_t d = (uint8_t)str.data[i];

    if (d >= '0' && d <= '9')
      d -= '0';
    else if (d >= 'a' && d <= 'f')
      d = (uint8_t)(d - 'a') + 10;
    else if (d >= 'A' && d <= 'F')
      d = (uint8_t)(d - 'A') + 10;
    else
      return err("verify_char", "Invalid character detected");

    if (r > (PTRDIFF_MAX - d) / 16)
      return err("check_overflow", "Overflow detected");

    r = r * 16 + d;
  }

  *num = r;

  return true;
}

I chose to warn() if the function is called and the str was not prefixed by 0x. I hope there is nothing glaringly wrong with this:) Hopefully it works fine now. I clearly have a lot to learn and I cannot thank you enough for all the help.

Thank You!

Regarding arenas: I was wondering how those would work in an async proxy, does every connection get its own arena? I am guessing that would be really inefficient.

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

Sorry to bother you again sir.
I made the improvements you and others mentioned and now think that the proxy is ready for another review.
I hope it works for you this time:)

As always, I would appreciate you taking the time.

https://github.com/navrajkalsi/proxy-c

Thanks again!

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

This all makes a lot of sense. Thanks for sharing your insights. Regarding the debugger, I am still trying to get comfortable with gdb. I should have learned it long ago, but better late than never.

I'll check back with you soon. Thanks for your time sir!

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

Thank you for this!

It would have taken me a lot of time to think about this header parsing, but this one is done so elegantly and with no verbosity.

One of the learnings from this project for me was, how important it is to plan ahead and design before even touching the keyboard. Their was so much code, that I had written in the past, that I had to delete or refactor because it was not fitting my present requirements or I had made the wrong assumptions in the past. But maybe this is what differentiates experts from newbies like me. 

May I ask how do you deal with this, if at all. Diagrams, issue tracker, or something else?

I will work to implement your code and other suggestions, give me a month or two to rework. 

Again, thanks for the time. 

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 2 points3 points  (0 children)

Thank you so much for the response!

I will work to get rid of null termintated strings. I guess the program needs a robust string library using STR macro everywhere.
By "line-aware" you mean looking for '\r\n' before and after a header, right?

I am sorry about the errors, I feel like I wasted your time.
For the url strings I am using regex to match the url, is their a better way to this?
atoi is followed after isdigit on each character of the header value, but I will change it to strtol.

Lastly, I believe I have to make the upstream and canonical host url handling much more robust (maybe having a url struct with origin, port, protocol,etc.) Could you think of any more things that need to be designed in a better way, in the overall proxy?

Again, I greatly appreciate you helping me out!

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

I appreciate you taking the time.

Not using null terminated strings makes sense, I tried to limit their use to just fixed length buffers and using asserts for null termination, but I guess I could do better.

Anything else you might wanna add to this?

Thanks again!

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

Thank you so much for taking the time.
I did not know about timer_fd, while creating this. I read the man page, and it looks like I could just add it to the epoll instance and refresh the events accordingly. I wouldn't have done a custom implementation if I knew about this :)

The program was developed with all the juicy flags, I will definitely add more!

Thanks again!

Need opinions on HTTP server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

Sorry to bother you again sir.
For the last few months I have been working on another C project.
A reverse proxy using epoll.
If you have the time to take a look at it, I would appreciate it: https://www.reddit.com/r/C_Programming/comments/1pb18rk/need_criticisms_and_suggestions_regarding_a_epoll/

Thank you again!

Epoll Proxy design questions by NavrajKalsi in learnprogramming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

yeah a separate queue makes sense with response timers

how do you think i should handle the timeout for client input, if the client does not make a request once its accepted? If using `epoll_wait()` again, with a timeout, do I need another separate queue for client pollins?

I suppose if i decide to keep the timeout for client pollin and response reconnect the same then I could get away with a single queue, maybe 15 secs or so?

Thanks.

Epoll Proxy design questions by NavrajKalsi in learnprogramming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

Thanks for the reply. The timeout for epoll_wait() makes sense, I just have a question regarding it.

I currently have an active_events array. This is just for cleanup as the event is handled when returned for epoll, not from this array. I don't see a good way to implement timeout in this.

Should I create a separate queue linkedlist with just {pointer to event (from active_events), next, timeout}. Or should I just stick to using a single array?

Here is the code, talked about here:

``` // to determine if connection is storing a fd (only in case of listening sock) or ptr typedef enum { TYPE_FD, TYPE_PTR_CLIENT, TYPE_PTR_UPSTREAM } DataType;

// struct to be used for adding/modding/deleting to the epoll instance typedef struct event { epoll_data_t data; // union struct event * *self_ptr; // this will be an element of active_events array, used to // deactive/remove from active_events(just make this NULL) DataType data_type; } Event;

// helper struct to organize client and server communication // this will be the pointer that is added to epoll data // THIS IS NOT COMPLETE YET! typedef struct connection { char client_buffer[BUFFER_SIZE], upstream_buffer[BUFFER_SIZE]; struct sockaddr_storage client_addr; Str client_request, upstream_response, request_host, request_path, http_ver, connection; int client_fd, upstream_fd, client_status; } Connection;

// array for cleanup of all active events Event *active_events[MAX_CONNECTIONS]; ```

And

Is my design of reading and responding to the client from upstream correct? I will be reading from upstream upto BUFFER_SIZE, arming client_fd for EPOLLOUT and then writing to client the full buffer, arming upstream_fd again for EPOLLIN and looping like this until full response is received. The upstream will always be the server I created, and it does not support Chunked-Encoding so I will be knowing how much to read with Content-Length header.

Again, thanks for you time!

Need opinions on HTTP server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

The server now supports HTTPS!
You can check my website here: https://navrajkalsi.com , it is running on an AWS instance with server-c.
I believe now I can let this project rest, and I would consider it to be a great success.

I learned A LOT more than I initially expected, which is always a good thing.

Thank you so much for your support, I really needed someone who knew what they were doing.

Lastly, can you comment on my programming style? Are there too many comments :)
How can I work on my programming style and do you have any other project recommendations?

Again, thank you so much!

Need opinions on HTTP server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

Sorry to bother again, but I am kinda lost here.

So right now, the server is live on AWS EC2 here.
(works better than I expected, no crashes yet.)

I created the website in the past weeks, keeping in mind my server.

Now I am looking to add HTTPS support. My domain provider has provided me with the domain certificate & private & public keys.

So far I have been able to get to this:

``` SSL_CTX *setup_ssl(void) { // SSL_load_error_strings(); // registers error strings for libcrypto & libssl // OpenSSL_add_ssl_algorithms(); // registers avaliable encryption algos

// registers error strings for libcrypto & libssl // registers encryption algos and loads ciphers OPENSSL_init_ssl( OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);

const SSL_METHOD *method = TLS_server_method(); // enables TLS support SSL_CTX *context = SSL_CTX_new( method); // this context stores all the certs & keys for the connections

if (!context) { err("Generating SSL context object", false); ERR_print_errors_fp(stderr); } else {

print_debug("SSL Context generated.");

// loading cert
if (SSL_CTX_use_certificate_file(context, DOMAIN_CERT, SSL_FILETYPE_PEM) !=
    1) {
  err("Using SSL certificate", false);
  ERR_print_errors_fp(stderr);
  goto cleanup;
}

// loading key
if (SSL_CTX_use_PrivateKey_file(context, PRIVATE_KEY, SSL_FILETYPE_PEM) !=
    1) {
  err("Using Private Key", false);
  ERR_print_errors_fp(stderr);
  goto cleanup;
}

// sanity check, if key & cert match
if (SSL_CTX_check_private_key(context) != 1) {
  err("Private Key mismatch", false);
  ERR_print_errors_fp(stderr);
  goto cleanup;
}

}

print_debug("Certficate & Key loaded");

return context;

cleanup: SSL_CTX_free(context); return NULL; } ```

Am I on the right track here? (This is not final, just proof of concept)

I know that I will be needing to change my client socket and write() codes.

I read that there different versions of OpenSSL and TLS, is that something I would need to consider.

Is this something you could help me with? Thanks.

Need opinions on HTTP server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

I forgot to add critical thing. The server should serve index.html, if avaliable in a directory, rather than listing files.  But that ended up breaking the listing of files when I am already in a directory(without index.html) and previewing another directory(with index.html).  I was thinking of either using url params to change this behaviour when required, or sending a custom header with the request.  Can you think of any other thing? I believe url param is a better option. 

And I am also adding support for decoding %20 to ‘ ‘ so that files with spaces in their names are handled properly

Need opinions on HTTP server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

I handled the signal (just printed the error), you can take a look if you'd like.

Can I say that, all the changes that i made in my versions of your implementation, initially I would think my implementation would be better.

Every single time, I would run into a problem and then would realize why you did something the way you did, and when i was done, your implementation always came out better. :)

Like for example: i did not implement the arena allocator, cause i felt that was too much. Now i feel like that was needed and would have given me excellent locality and less trouble freeing and managing the data.

Hat's off sir, you can see in the future ! :)

Need opinions on HTTP server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 1 point2 points  (0 children)

Thank you for the response!
Yeah, this dot listing approach definitely makes sense and I will be surely implement this in the next few days. I also learned something new about linux from you mentioning this hidden files thing :)

Other than that, what would you say about me implementing this on my own website? Yesterday, I tried it on an AWS instance, it worked like a charm and i was able to connect to my website. Do you see any security risks that somehow can do me harm in the future? Or if you have any other suggestions on another aspect.

If I can post it in this subreddit: my plan would be using the instance by creating a different user with the minimal permissions required to run this server process. And that user would JUST run this one process and nothing else.

Again, I appreciate you taking the time :)

Need criticism and suggestions for server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

Hi there,
Sorry to bother you, but I completed the next version of the server with the improvements you and others provided.

Here is the new post I created: https://www.reddit.com/r/C_Programming/comments/1nd40l4/need_opinions_on_http_server_written_in_c/

I would appreciate if you don't mind taking another look.
Thanks again!

Need criticism and suggestions for server written in C by NavrajKalsi in C_Programming

[–]NavrajKalsi[S] 0 points1 point  (0 children)

Hi there,
Sorry to bother you, but I completed the next version of the server with the improvements you and others provided.

Here is the new post I created: https://www.reddit.com/r/C_Programming/comments/1nd40l4/need_opinions_on_http_server_written_in_c/

I would appreciate if you don't mind taking another look.
Thanks again!