« first day (3633 days earlier)      last day (1298 days later) » 

1:31 AM
 
 
4 hours later…
5:26 AM
Hi.

1) How can I store a file in a char variable?

2) How can I get the exact size of a file in C? I used different methods, but it returns 89 bytes for a 91 bytes file. I don't know why. I sued ftell.
I've written a simple webserver. I need to return a complete file to the client socket.

// Connection Response
char response_data[file_size];
while (!feof(document)) {
char line[file_size];
fgets(line, file_size, document);
strcat(response_data, line);
}

I used this script. Is this ok? I got the file size using ftell, but as I told you it returns a wrong number.
 
 
1 hour later…
6:39 AM
@X4748-IR Are these text files? Sounds like a CRLF effect. (Windows linebreaks have 2 bytes, other operating systems only use 1 byte for linebreaks).
Your array will probably blow the stack for files larger than a couple MBs.
Also, your solution only works for text files. (fgets reads a line of text, strcat appends text etc.)
It won't work for binary files with 0-bytes in them, because text functions treat those as end-of-string.
 
 
6 hours later…
12:35 PM
So at today's AI in Robotics meetup, we have someone demo touch sensors, but it's only after the presentation, I thought about this:
Mar 31 '17 at 9:58, by Telkitty
I want to see butt authentication chair - the chair that can tell your identity from pressure and the sharp of your butt!
Suddenly an excellent idea hit me - such a sensor would be prefect for butt authentication chair!
And then a tsunami of ideas hit me! :x
I mean you could reach world domination with this butt profiling thing - just collect all the butt shape data, and convince everyone that butt profiling is more accurate then face profiling :x </trollololo>
Telkitty - generating world domination ideas since 19xx!
(runs away before someone chases me with a broom)
 
 
1 hour later…
2:11 PM
@fredoverflow It's an HTML file.
@fredoverflow You're right, but what's the solution? It's killing me. I don't know what to do ): I'm not that familiar with C, everything becomes so hard in this language
Is this right that all the web servers keep the data in the memory and then they return the file to the user socket?
 
 
1 hour later…
3:14 PM
It is said that literal strings are lvalues. In order to prove this statement, I want to make an assignment with "helloworld" on the left side. How to do so?
 
Solved it! The solution was the dynamic memory allocation! I used malloc and it solved.
 
@X4748-IR: Please show me your code!
 
3:50 PM
@NotAZoomedImage There you go:
    // Response Header
    char http_header[] = "HTTP/1.1 200 OK\r\n\n";

    // Connection Response
    char *response_data = (char *) malloc(file_size + sizeof(http_header));
    while (!feof(document)) {
        char line[file_size];
        fgets(line, file_size, document);
        strcat(response_data, line);
    }
    prepend(response_data, http_header);
It seems all the problems were because of the response packet size. Clearly, I couldn't get the correct size
 
4:30 PM
@X4748-IR what's with the "char http_header[] = "HTTP/1.1 200 OK\r\n\n";" ? Isn't http always "\r\n" ?
I don't think I've seen the double \n\n in http
also the same as always applies with fopen, under windows don't forget the 'b' openmode flag
 
@X4748-IR Why do you read the file line by line with many fgets calls? Just read the whole file with one fread call.
@X4748-IR Also, malloc returns uninitialized memory, strcat tries to find the NUL terminator in random garbage, that can't possibly work.
@PeterT http mandates Windows linebreaks? TIL
 
5:29 PM
@PeterT The client browser needs 2 bytes of \x0a to separate the content of the page and the headers
@fredoverflow Thanks. Fixed it.
@fredoverflow Why no? It initializes the memory as long as the file content + the length of the headers.
As it seems the hardest thing about making a web server is the memory management
@PeterT + 1 for separating each header and the other one for separating the document content
 
 
2 hours later…
7:35 PM
@X4748-IR right two newlines for the content, but lines only ever get seperated by \r\n in http
so it would be \r\n\r\n
 
 
2 hours later…
9:05 PM
@X4748-IR Immediately after char *response_data = (char *) malloc(file_size + sizeof(http_header));, what do you think the value of response_data[0] is?
@X4748-IR That's why sane people write web servers in garbage collected languages ;)
 
 
1 hour later…
user1804599
10:32 PM
Don’t write networking code in memory-unsafe languages 😫
 
10:49 PM
So, I done screwed up. Was interviewing for a senior/principal engineering role. As a joke proposed to do some problem they were working on as part of the interview coding test. Received a rather difficult algorithm problem that needs to be optimized. Basically going to spend a few days doing work I normally get paid $85 an hour for consulting :-)
Gotta write some GPU optimized version that appears to resemble part of numpy's einsum function
 
11:13 PM
Hmmm. I've changed the code a bit. I read your message again. I got what you mean, almost!

// Connection Response
char *response_data = (char *) malloc(file_size + sizeof(http_header));
fread(response_data, file_size, 1, document);
prepend(response_data, http_header);

I think the allocated file-length memory is going to be filled with the data inside the file. What do you expect? Should've been filled at the allocation time?
@fredoverflow I don't think that's always true. That's right it's really hard to manage the memory using the C language, but on the other hand, it becomes so efficient and fast in the production mode. As you surely know, the nginx is written in C.
 
11:28 PM
I was reading my network packets. It was like what you say. 0x0D0A. Thanks.
Is there any article about such things? If you have something in mind, please send the link! ;)
I found something: https://www.tutorialspoint.com/http/index.htm


A Start-line

Zero or more header fields followed by CRLF

An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields

Optionally a message-body
 

« first day (3633 days earlier)      last day (1298 days later) »