Programming a web server in C

G

Georgios Sakalis

I need some help for proect of mine

I am bulding a web server in c

To parse the request I am using a char *buffer, with a default size at te
begining, If the bytes that been read are equal to that size I realloc the
buffer and so on.

The problem is when I am trying to implement file uploading. I read also the
file in the buffer, but the problem is that i cannot find the size of the
binary file in order to say how many bytes should write to the file.

Any ideas or code??
 
M

Malcolm

Georgios Sakalis said:
I need some help for proect of mine

I am bulding a web server in c

To parse the request I am using a char *buffer, with a default size at te
begining, If the bytes that been read are equal to that size I realloc the
buffer and so on.

The problem is when I am trying to implement file uploading. I read also
the file in the buffer, but the problem is that i cannot find the size of
the binary file in order to say how many bytes should write to the file.

Any ideas or code??

Good enough code

long getfilesize(char *path)
{
FILE *fp;
long answer;

fp = fopen(path, "rb");
if(!fp)
return -1;
fseek(fp, 0, SEEK_END):
answer = ftell(fp);
fclose(fp);

return answer;
}

Add a few bytes to your buffer for luck.
The code will work, but unfortunately falls foul of a few caveats in the
standard, there to support really huge files and stuff like that.
 
M

Morris Dovey

Georgios Sakalis (in [email protected]) said:

| I need some help for proect of mine
|
| I am bulding a web server in c
|
| To parse the request I am using a char *buffer, with a default size
| at te begining, If the bytes that been read are equal to that size
| I realloc the buffer and so on.
|
| The problem is when I am trying to implement file uploading. I read
| also the file in the buffer, but the problem is that i cannot find
| the size of the binary file in order to say how many bytes should
| write to the file.
|
| Any ideas or code??

For uploading binary files, you might do well to use fixed-size
buffers obtained as needed from a pool of available empty buffers.
Buffers can be flushed to a disk file as needed to prevent an attacker
from eating all of server memory, causing a buffer overrun, etc.

comp.lang.c is for discussion of C - not for discussion of all of the
things one might /do/ with C. A better source of information would
probably be a networking newsgroup that focuses on your platform.
 
J

jmcgill

Georgios said:
The problem is when I am trying to implement file uploading. I read also the
file in the buffer, but the problem is that i cannot find the size of the
binary file in order to say how many bytes should write to the file.

Any ideas or code??

I gather you are not requiring a Content-Length header on PUT requests.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top