Skip to content

add mmap_file for WIN32 and httpserver example improvements#3241

Merged
lerno merged 8 commits into
c3lang:masterfrom
ManuLinares:httpserver-example-v2
May 28, 2026
Merged

add mmap_file for WIN32 and httpserver example improvements#3241
lerno merged 8 commits into
c3lang:masterfrom
ManuLinares:httpserver-example-v2

Conversation

@ManuLinares
Copy link
Copy Markdown
Member

Can't add many features for this example of course

  • hardened against directory traversal, and a bunch of other optimizations, it's still small and pretty.
  • added description
<*
 This is a simple static HTTP server example. It listens on a TCP port, parses basic
 HTTP/1.1 requests, handles Keep-Alive connections, serves static files with Range
 and ETag support, applies dynamic Gzip compression, and displays directory listings.
*>

also hardened against directory traversal
`lib/std/io/file_mmap.c3`: prevent view size from exceeding file mapping
bounds.
Comment thread lib/std/core/os/mem_vm.c3 Outdated
Win32_HANDLE h_file = (Win32_HANDLE)win32::_get_osfhandle(fd);
if (h_file == (void*)uptr::max) return io::FILE_NOT_VALID~;

bool is_write = access == WRITE || access == READWRITE;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXECWRITE? Why not use to_win32 and tweak it?

Comment thread lib/std/core/os/mem_vm.c3

Win32_DWORD offset_high = (Win32_DWORD)(offset >> 32);
Win32_DWORD offset_low = (Win32_DWORD)(offset & 0xFFFFFFFF);
void* ptr = win32::mapViewOfFile(h_map, map_access, offset_high, offset_low, (Win32_SIZE_T)size);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the size = 0 case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile

[in] dwNumberOfBytesToMap

The number of bytes of a file mapping to map to the view. All bytes must be within the maximum size specified by CreateFileMapping. If this parameter is 0 (zero), the mapping extends from the specified offset to the end of the file mapping.

For zero it says is valid, and maps from the offset to the end. I could change the contract comment to reflect this?

@param size : "Number of bytes to map, will be rounded up to page size"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for our api consistency we could, on size == 0 do aligned_alloc_size which does one win32::allocation_granularity, I don't know what's the correct path here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding is that POSIX mmap requires the length argument be strictly greater than zero, we are rounding up now to 1 pagesize when mmap_file
maybe in posix we should also if (size == 0) return INVALID_ARGS~;

@LowByteFox
Copy link
Copy Markdown
Contributor

👀 something quite useful for me

@lerno lerno merged commit 6cd3438 into c3lang:master May 28, 2026
16 checks passed
@lerno
Copy link
Copy Markdown
Collaborator

lerno commented May 28, 2026

Thank you!

@ManuLinares ManuLinares deleted the httpserver-example-v2 branch May 28, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants