#My server is working on mac but broken on linux

74 messages · Page 1 of 1 (latest)

serene zealot
#

On mac

Listening on port 8001
Looking up for variable name: notes
notes == notes?
Response written successfully!, 1258 bytes written
mime type text/css
Response written successfully!, 1055 bytes written
mime type image/gif
Response written successfully!, 3766770 bytes written
Looking up for variable name: notes
notes == notes?
Response written successfully!, 1258 bytes written
mime type text/css
Response written successfully!, 1055 bytes written
mime type image/gif
Response written successfully!, 3766770 bytes written
Looking up for variable name: notes
notes == notes?
Goes the same
Listening on port 8001
Looking up for variable name: notes
notes == notes?
Response written successfully!, 1258 bytes written
mime type text/css
Response written successfully!, 1055 bytes written
mime type image/gif
Response written successfully!, 3766770 bytes written
Looking up for variable name: notes
notes == notes?
Response written successfully!, 3766770 bytes written
Looking up for variable name: notes
notes == notes?
Response written successfully!, 3766770 bytes written
Looking up for variable name: notes
notes == notes?
Response written successfully!, 3766770 bytes written
Response written successfully!, 3766777 bytes written
Response written successfully!, 3766777 bytes written
Response written successfully!, 3766777 bytes written

After one request it brokes on linux.
When we try to enter into another page it still thinks we try to get it still tryies to write the image. extra 7 bytes??
I'm coding for years but this is really w*f?

source code;
https://github.com/wizard-lgtm/end

GitHub

end of my pain. Contribute to wizard-lgtm/end development by creating an account on GitHub.

sullen spindleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

shy creek
#

I just ran it through fsanitize and it gave me nasty crash stating that there was a call to malloc() with a rediculous size. I'll have a look at the source and tell you if I find anything.

serene zealot
#

this is getting interesting

#

i didn't try clang sanitilizer yet

shy creek
#

It looks like you're not initializing response -> body_length.

#

@serene zealot

serene zealot
#

oh yeah

serene zealot
#

it's appending if it's not set

shy creek
serene zealot
#

does it work after initializing body length as zero

shy creek
#

When I run the server and connect in my browser it's all grey and I get the following output in the server console.

Looking up for variable name: notes
notes == notes?
Response written successfully!, 65 bytes written```
serene zealot
#

65 bytes

#

does it render index.html?

shy creek
#

It's just... grey

serene zealot
#

it's..

#

grey..

shy creek
#

Yeah.......

serene zealot
#

so do you have a folder called "pages"

shy creek
#

Yes

#

And it does in fact contain index.html.

serene zealot
#
    char* body = template_render_template("./pages/index.html", data);
    if (!body) {
        perror("template_render_template failed");
        template_free_list(data);
        free_posts(notes);
        free(notes_str);
        http_route_500(req, res);
        return;
    }
    printf("rendered template %s\n", body);
#

could you add
printf("rendered template %s\n", body);

about line 638

#

so we can see it's rendering or not

#

everyting is works fine on me

#

btw what you think about the code quality?

shy creek
#

This is what I get

  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hi</title>
    <link rel="stylesheet" href="./public/style.css">

  </head>
  <body>
    <h1>Welcome to the End of My Sufferings</h1>
    <img src="./public/plane.gif" alt="Plane GIF">
    <p>i believe in the old internet</p>
    <h2>Posts</h2>
    {for note in notes}
      <li>{note}</li>
    {endfor}
    <h1>SSDA</h1>
    <p>
      Info: No JavaScript is used in this website. Fully rendered in C template.
      <a href="https://www.youtube.com/watch?v=J6yiNbiiCzA">Because JS sucks</a>
      <hr />
      <a href="/source">source</a>
      <a href="/gallery">gallery</a>
    </p>

    <i>Created: 03.01.2025 | Last Edited: 09.01.2025</i>

    <!-- Marquee Text -->
    <marquee behavior="scroll" direction="left" scrollamount="10">
      Welcome to the Retro Web!
    </marquee>

    <div class="footer">
      <p>
        This website is designed to run on any primitive browser. Experience the nostalgia of the old internet suck it v8!
      </p>
    </div>
  </body>
</html>```
serene zealot
#

it renders quite well

#

an issue happening over sending to client

#

have you ever changed some code?

#

res->body_length = strlen(body);
try adding this

shy creek
#

The only thing I changed was this on line 1145:
response -> body_length = 0;

#

Which is what fixed the crash

serene zealot
shy creek
serene zealot
shy creek
serene zealot
#

after you're setting the body as

#

res->body = strdup(body);
res->body_length = strlen(body);
if (!res->body) {
perror("Failed to allocate memory for response body");
free(body);
free_posts(notes);
free(notes_str);
http_route_500(req, res);
return;
}

#

this lines of code

#

640

#

void http_complete_connection(struct timespec start, Response* response, int client_fd) {
    printf("Debug: body: %s\n", response->body);

#

this debuger also clarifies that body is still okay in completing connection

#

if it's still the same value we're okay with that

#

i'll look into code after getting my computer

shy creek
#

Looks good to me!

#

gallery leads to more grey, not sure if that's intended

serene zealot
#

gallery is not set yet

shy creek
#

I see

serene zealot
#

so the body length causes 0

#

that causes only gray

#

try also setting body length on
http_route_404

#

so the 404 page will work as well

#

that was such a dumb error wasn't it

#

what you think about the code quality?

shy creek
#

In terms of readability you mean?

#

Also here's the error page.

serene zealot
#

also security

#

btw the all issues are happening because of the default response values

you have to set
response->content_type and
response->body_length in route functions

#
void http_route_404(Request* req, Response* res){
        res->version = strdup("HTTP/1.1");
        res->status_code = strdup("404");
        res->status_message = strdup("NOT FOUND");

        char* page = file_read_to_buffer("./pages/404.html");
        res->body = strdup(page);
        res->body_length = strlen(page);
        res->content_type = MIME_TEXT_HTML;
        printf("body length%d\n", res->body_length);
        if (res->body == NULL) {
            perror("Failed to allocate memory for response body");
        }
        res->headers = NULL; 
        free(page);
}
#

I guess i have to do a response init function

#
    if(
        response->content_type == MIME_TEXT_HTML || 
        response->content_type == MIME_TEXT_PLAIN || 
        response->content_type == MIME_TEXT_CSS
    ){
        // Append the body length if it's not 
        if(!response->body_length){
            response->body_length = strlen(response->body);
        }
    }
    // Set unknown mime if it's not set for security
    if(response->content_type == UNKNOWN){
        printf("Mime type is unknown! Setting to octet-stream\n");
        response->content_type = MIME_APPLICATION_OCTET_STREAM;
    }

#

added safety features

serene zealot
#

problem fix by that commit

#

can be closed

#

!solved

sullen spindleBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity