rohanasan 0.4.60

An extremely fast backend framework for rust! Built from scratch, easy to use and asynchronous. Available for multiple programming languages and platforms like unix, linux, mac, FreeBSD. Made using web sockets.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// MIT License
//
// Copyright (c) 2024 Rohan Vashisht
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


//! # Rohanasan: An extremely fast backend framework built for rust
//!
//! - Made with Performance, optimization and ease of use in mind.
//!
//! - Currently available in C/C++/Rust programming languages only.
//!
//! - Please use a linux/unix/mac kind of os.
//!
//! - This library has been built from scratch.
//!
//! # How to use in your project?
//! - Open terminal inside the parent folder where you would like to create the folder of your project
//! - Run:
//! ```shell
//! cargo new myproj
//! cd myproj
//! cargo add rohanasan
//! ```
//! - For a start you can add this to main.rs:
//!
//! ```no_run
//! use rohanasan::{init, send_http_response, serve, Request, DEFAULT_HTML_HEADER, ERROR_404_HEADER, rohanasan};
//!
//! fn handle(request:Request) -> String {
//!     if request.path == "/" {
//!         send_http_response(DEFAULT_HTML_HEADER, "<h1>Thanks for choosing Rohanasan-rs!</h1>")
//!     }
//!     else{
//!         send_http_response(ERROR_404_HEADER, "<h1>404!</h1>")
//!     }
//! }
//!
//! fn main() {
//!     println!("Listening at http://localhost:8080");
//!     rohanasan!{
//!         serve(init(8080), handle).await;
//!     }
//! }
//! ```
//! - `cargo run` to run your project.
//! - Go to: `localhost:8080`.
//! - Enjoy using Rohanasan!
//!
//! # How to run the example?
//! ```shell
//! git clone https://github.com/rohanasan/rohanasan-rs.git
//! cd rohanasan-rs
//! cd examples
//! cargo run --example example
//! ```
//!
//! ## Discord server link:
//! [https://discord.gg/Yg2A3mEret](https://discord.gg/Yg2A3mEret)
//!
//! ### Current Features:
//! - Can run a server at a specified port
//! - Can serve a folder named static at /static
//! - Can send files as http response
//! - Can give you the path, method and protocol
//! ### TODO:
//! - Add feature to change the directory path of the public folder ☑️ Done!!!!
//! - Asynchronous file request handling ☑️ Done!!!!
//! - Add feature to give the user an option to add index.html to static folder ☑️ Done!!!! you can send ./html/static_index.html
//! - Add statistics of performance.
//! - Add feature to... currently it's just a pre alpha release I have to add a lot of features right now!
//!
//! ### Contribute:
//! [https://www.buymeacoffee.com/rohanvashisht](https://www.buymeacoffee.com/rohanvashisht)
//!
//! Please star rohanasan's github repo:
//!
//! [https://github.com/rohanasan/rohanasan-rs](https://github.com/rohanasan/rohanasan-rs)
//! # Example
//! ## Hello world (Html):
//! > Basic Html implementation of hello world:
//! ```no_run
//! use rohanasan::{rohanasan, serve, init, Request, send_http_response, DEFAULT_HTML_HEADER};
//!
//! fn handle(request: Request) -> String {
//!     send_http_response(DEFAULT_HTML_HEADER, "<h1>Hello, World</h1>")
//! }
//!
//! fn main() {
//!     rohanasan!{
//!         serve(init(8080), handle).await;
//!     }
//! }
//! ```
//! ## Hello world (Html File):
//! > Basic Html implementation of hello world:
//! ```no_run
//! use rohanasan::{rohanasan, serve, init, Request, send_file, DEFAULT_HTML_HEADER};
//!
//! fn handle(request: Request) -> String {
//!     send_file(DEFAULT_HTML_HEADER, "./html/index.html")
//! }
//!
//! fn main() {
//!     rohanasan!{
//!         serve(init(8080), handle).await;
//!     }
//! }
//! ```
//! # Points to remember:
//! - There is no need to import async_std for using rohanasan macro.
//! - There is no need to import url-decode for using decode funciton
//! - By default rohanasan serves any folder named static present in the same directory where you are running the server.

pub use async_std::task::block_on;
use libc::{accept, bind, c_char, c_int, c_void, close, in_addr, listen, recv, sa_family_t, send, setsockopt, sockaddr, sockaddr_in, socket, socklen_t, AF_INET, INADDR_ANY, SOCK_STREAM, SOL_SOCKET, SO_REUSEPORT, puts};
use std::ffi::{CStr, CString};
use std::fs::File;
use std::io::Read;
use std::mem::size_of;


/// # Use this macro to use .await with the serve function
/// ## Usage:
/// ```usage
/// rohanasan! {
///      serve(init(8080), handle).await;
/// }
/// ```
/// ## Example:
/// ```rust
/// use rohanasan::{init, Request, serve, rohanasan, send_http_response, DEFAULT_HTML_HEADER};
///
/// fn handle(request: Request) -> String {
///     send_http_response(DEFAULT_HTML_HEADER, "<h1>Hello world!</h1>")
/// }
///
/// fn main() {
///     rohanasan! {
///         serve(init(8080), handle).await;
///     }
/// }
/// ```
/// **This is basically a wrapper for `async_std::task::block_on(async{/* your content here */})`**
///
/// ## Important:
/// There is no need to import async_std in your rohanasan project.
#[macro_export]
macro_rules! rohanasan {
    // Define the macro pattern.
    ($($body:tt)*) => {
        use $crate::block_on as why_will_someone_use_this_as_a_name_to_import_task_32194ilqrjf8da;
        // Use async-std task spawning to run the asynchronous block provided.
        why_will_someone_use_this_as_a_name_to_import_task_32194ilqrjf8da(async {
            $($body)*
        });
    };
}

const STATIC_FOLDER: &str = "./static/";
/// This is the Default Html Header.
pub const DEFAULT_HTML_HEADER: &str = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";

/// This is the Default Json Header.
pub const DEFAULT_JSON_HEADER: &str = "HTTP/1.1 200 OK\nContent-Type: application/json\n\n";

/// This is the Default 403 error header
pub const ERROR_403_HEADER: &str = "HTTP/1.1 403 Forbidden\nContent-Type: text/html\n\n";

/// This is the Default Plain Texts' Header
pub const DEFAULT_PLAIN_TEXT_HEADER: &str = "HTTP/1.1 200 OK\nContent-Type: text/plain\n\n";

/// This is the Default 500 errors' header
pub const DEFAULT_500_HEADER: &str = "HTTP/1.1 500 Internal Server Error\nContent-Type: text/html\n\n";

/// This is the Default 404 errors' Header.
pub const ERROR_404_HEADER: &str = "HTTP/1.1 404 Not Found\nContent-Type: text/html\n\n";

/// This is the default 301 permanently moved error's header.
pub const DEFAULT_301_HEADER: &str = "HTTP/1.1 301 Moved Permanently\nContent-Type: text/html\n\n";

/// This is the default 400 errors' header.
pub const DEFAULT_400_HEADER: &str = "HTTP/1.1 400 Bad Request\nContent-Type: text/html\n\n";

/// This is the default 401 unauthorized errors' header.
pub const DEFAULT_401_HEADER: &str = "HTTP/1.1 401 Unauthorized\nContent-Type: text/html\n\n";

/// This is the default 402 payment required errors' header.
pub const DEFAULT_402_HEADER: &str = "HTTP/1.1 402 Payment Required\nContent-Type: text/html\n\n";

/// ### Take this as a parameter in your handle function
/// **This contains 5 things:**
/// 1) `path`: This contains the path that the person requested like /hello or /something.
/// 2) `method`: This contains the method used to send the request like: GET or POST.
/// 3) `get_request`: This contains the GET requests' parameters (if GET request was made) like: ?q=something%20awesome.
/// 4) `protocol`: This contains the protocol used to make the HTTP request like:
/// 5) `post_request`: This contains the POST requests' parameter (if POST request was made) like: {something:something}.
pub struct Request {
    /// path: This contains the path that the person requested like /hello or /something.
    pub path: &'static str,
    /// method: This contains the method used to send the request like: GET or POST.
    pub method: &'static str,
    /// get_request: This contains the GET requests' parameters (if GET request was made) like: ?q=something%20awesome.
    pub get_request: &'static str,
    /// protocol: This contains the protocol used to make the HTTP request like:
    pub protocol: &'static str,
    /// post_request: This contains the POST requests' parameter (if POST request was made) like: {something:something}.
    pub post_request: &'static str,
}

/// # Use this function to initialize rohanasan backend framework.
/// **Provide this a port datatype: `u16`,**
/// ## Usage:
/// ```usage
/// rohanasan! {
///      serve(init(8080), handle).await;
/// }
/// ```
/// ## Example:
/// ```rust
/// use rohanasan::{init, Request, serve, rohanasan};
///
/// fn handle(request: Request) -> String {
///     "Hello!".parse().unwrap()
/// }
///
/// fn main() {
///     rohanasan! {
///         serve(init(8080), handle).await;
///     }
/// }
/// ```
#[cfg(not(target_os = "linux"))]
pub fn init(port: u16) -> (i32, sockaddr_in, usize) {
    let opt: c_int = 1;

    let server_fd: c_int = unsafe { socket(AF_INET, SOCK_STREAM, 0) };
    if server_fd == -1 {
        panic!("Failed to create socket");
    }
    let address: sockaddr_in = sockaddr_in {
        sin_family: AF_INET as sa_family_t,
        sin_port: unsafe { htons(port) },
        sin_addr: in_addr { s_addr: INADDR_ANY },
        sin_zero: [0; 8],
        sin_len: 1,
    };
    let addrlen: usize = size_of::<sockaddr_in>();
    let res: c_int = unsafe {
        setsockopt(
            server_fd,
            SOL_SOCKET,
            SO_REUSEPORT,
            &opt as *const i32 as *const c_void,
            std::mem::size_of_val(&opt) as socklen_t,
        )
    };
    if res == -1 {
        panic!("Failed to set socket option");
    }
    (server_fd, address, addrlen)
}

/// # Use this function to initialize rohanasan backend framework.
/// **Provide this a port datatype: `u16`,**
/// ## Usage:
/// ```ignore
/// rohanasan! {
///      serve(init(8080), handle).await;
/// }
/// ```
/// ## Example:
/// ```no_run
/// use rohanasan::{init, Request, serve, rohanasan};
///
/// fn handle(request: Request) -> String {
///     "Hello!".parse().unwrap()
/// }
///
/// fn main() {
///     rohanasan! {
///         serve(init(8080), handle).await;
///     }
/// }
/// ```
#[cfg(target_os = "linux")]
pub fn init(port: u16) -> (i32, sockaddr_in, usize) {
    let opt: i32 = 1;

    let server_fd: i32 = unsafe { socket(AF_INET, SOCK_STREAM, 0) };
    if server_fd == -1 {
        panic!("Failed to create socket");
    }
    let address: sockaddr_in = sockaddr_in {
        sin_family: AF_INET as sa_family_t,
        sin_port: unsafe { htons(port) },
        sin_addr: in_addr { s_addr: INADDR_ANY },
        sin_zero: [0; 8],
    };
    let addrlen: usize = size_of::<sockaddr_in>();
    let res: i32 = unsafe {
        setsockopt(
            server_fd,
            SOL_SOCKET,
            SO_REUSEADDR,
            &opt as *const i32 as *const c_void,
            size_of::<i32>() as socklen_t,
        )
    };
    if res == -1 {
        panic!("Failed to set socket option");
    }
    (server_fd, address, addrlen)
}

/// # Use this function to serve the initialized port according to handle.
/// **Provide this the value returned by the init function and a handle function as well.**
/// ## Usage:
/// ```ignore
/// rohanasan! {
///     serve(init(8080), handle).await;
/// }
/// ```
/// ## Example:
/// ```no_run
/// use rohanasan::{init, Request, serve, rohanasan};
///
/// fn handle(request: Request) -> String {
///     "Hello!".parse().unwrap()
/// }
///
/// fn main() {
///     rohanasan! {
///         serve(init(8080), handle).await;
///     }
/// }
/// ```
pub async fn serve<F>(args: (i32, sockaddr_in, usize), func: F)
where
    F: Fn(Request) -> String + Send + Sync + 'static + Copy,
{
    let (server_fd, address, addrlen) = args;
    let if_bind: i32 = unsafe {
        bind(
            server_fd,
            &address as *const _ as *const sockaddr,
            addrlen as socklen_t,
        )
    };
    if if_bind == -1 {
        panic!("Failed to bind");
    }
    let if_listen = unsafe { listen(server_fd, 3) };
    if if_listen == -1 {
        panic!("Failed to listen");
    }
    loop {
        let new_socket: i32 = unsafe {
            accept(
                server_fd,
                &address as *const _ as *mut sockaddr,
                &addrlen as *const _ as *mut socklen_t,
            )
        };
        if new_socket == -1 {
            continue;
        }
        async_std::task::spawn(async move {
            let mut buf: [c_char; BUFFER_SIZE] = [0; BUFFER_SIZE]; // Allocate buffer

            unsafe {
                recv(
                    new_socket,
                    buf.as_mut_ptr() as *mut c_void,
                    BUFFER_SIZE - 1,
                    0,
                );
                puts(buf.as_ptr());
            }
            let mut x = String::from_utf8(buf.iter().map(|i| *i as u8).collect::<Vec<u8>>());
            match x {
                Ok(x) => {
                    let tokens = x.leak().split_whitespace().collect::<Vec<&str>>();
                    let method = tokens[0];
                    let mut path: &str = "";
                    let mut get_request = "";
                    let mut post_request = "";
                    let mut protocol = "";
                    if tokens.len() > 2 {
                        path = tokens[1].split('?').collect::<Vec<&str>>()[0];
                        if path.ends_with('/') && path != "/" {
                            path = &path[0..path.len() - 1];
                        }
                        if tokens[1].split('?').collect::<Vec<&str>>().len() > 1 {
                            get_request = tokens[1].split('?').collect::<Vec<&str>>()[1];
                        } else {
                            get_request = "";
                        }
                        protocol = tokens[2];
                        if method == "POST" {
                            post_request = tokens[tokens.len() - 1];
                        }
                    }
                    let the_thing_we_need_to_give_to_func = Request {
                        path,
                        method,
                        get_request,
                        protocol,
                        post_request,
                    };
                    if path.starts_with("/static/") && path != "/static/" && path != "/static" {
                        let mut file_path = String::from(STATIC_FOLDER);
                        file_path.push_str(&path[8..]);
                        // println!("{}", file_path);

                        let file_path_cstr = CString::new(file_path).expect("Invalid file path");
                        serve_static_file(new_socket, file_path_cstr.as_ptr());
                        unsafe {
                            close(new_socket);
                        }
                    } else {
                        let response = func(the_thing_we_need_to_give_to_func);
                        unsafe {
                            send(
                                new_socket,
                                response.as_ptr() as *const c_void,
                                response.len(),
                                0,
                            ); // Use the correct length
                            close(new_socket);
                        }
                    }
                }
                Err(_) => {
                    // If conversion fails, handle the error
                    let response = String::from("URL format not in utf8 format");
                    unsafe {
                        send(
                            new_socket,
                            response.as_ptr() as *const c_void,
                            response.len(),
                            0,
                        ); // Use the correct length
                        close(new_socket);
                    }
                    // You may choose to return early, log the error, or take any other appropriate action here.
                }
            }
        });
    }
}

/// This is the default buffer size used in rohanasan's development majorly.
pub const BUFFER_SIZE: usize = 1024;

// Function to serve static files

extern "C" {
    fn htons(p0: u16) -> u16;
}

/// # Use this function to send a file.
/// **Provide this a header and a path to a html file.**
/// ## Usage:
/// ```ignore
/// send_file(DEFAULT_HTML_HEADER,"./html/index.html")
/// ```
/// ## Example:
/// ```no_run
/// use rohanasan::{init, Request, serve, DEFAULT_HTML_HEADER, send_file, ERROR_404_HEADER, rohanasan};
///
/// fn handle(request: Request) -> String {
///     if request.path == "/"{
///         send_file(DEFAULT_HTML_HEADER,"./html/index.html")
///     }
///     else {
///         send_file(ERROR_404_HEADER ,"./html/404.html")
///     }
/// }
/// fn main() {
///     rohanasan! {
///         serve(init(8080), handle).await;
///     }
/// }
/// ```
pub fn send_file(header: &str, file_path: &str) -> String {
    let mut file = File::open(file_path).expect("Please enter the correct path to your html file");
    let mut contents = String::new();
    file.read_to_string(&mut contents)
        .expect("File can't be read!");
    send_http_response(header, &contents)
}

/// # Use this function to send an HTTP response.
/// **Provide this function header:&str and a body:&str.**
/// ## Usage:
/// ```ignore
/// send_http_response(DEFAULT_HTML_HEADER, "<h1>Hello!</h1>")
/// ```
/// ## Example:
/// ```no_run
/// use rohanasan::{init, Request, serve, DEFAULT_HTML_HEADER, send_http_response, ERROR_404_HEADER, rohanasan};
///
/// fn handle(request: Request) -> String {
///     if request.path == "/"{
///         send_http_response(DEFAULT_HTML_HEADER, "<h1>Hello!</h1>")
///     }
///     else {
///         send_http_response(ERROR_404_HEADER ,"<h1>404</h1>")
///     }
/// }
/// fn main() {
///     rohanasan! {
///         serve(init(8080), handle).await;
///     }
/// }
/// ```
pub fn send_http_response(header: &str, body: &str) -> String {
    let thing: String = header.to_string().clone() + body;
    thing
}
fn serve_static_file(client_socket: c_int, file_path: *const c_char) {
    let file_path_str = unsafe { CStr::from_ptr(file_path).to_string_lossy() };
    let file_path = file_path_str.trim();

    // Attempt to open the file
    let mut file = match File::open(file_path) {
        Ok(file) => file,
        Err(_) => {
            let not_found_response =
                b"HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<h1>404 Not Found</h1>";
            unsafe {
                send(
                    client_socket,
                    not_found_response.as_ptr() as *const c_void,
                    not_found_response.len(),
                    0,
                );
                close(client_socket);
            }
            return;
        }
    };

    // Determine content type based on file extension
    let content_type = determine_content_type(file_path);

    // Send HTTP response header with correct content type
    let response_header = format!("HTTP/1.1 200 OK\r\nContent-Type: {}\r\n\r\n", content_type);
    let response_header_cstr =
        CString::new(response_header.clone()).expect("Failed to create response header CString");
    unsafe {
        send(
            client_socket,
            response_header_cstr.as_ptr() as *const c_void,
            response_header.len(),
            0,
        )
    };

    // Send file content
    let mut buffer = [0; BUFFER_SIZE];
    loop {
        match file.read(&mut buffer) {
            Ok(0) => break,
            Ok(bytes_read) => {
                unsafe {
                    send(
                        client_socket,
                        buffer.as_ptr() as *const c_void,
                        bytes_read,
                        0,
                    )
                };
            }
            Err(_) => {
                eprintln!("Error reading file");
                break;
            }
        }
    }

    unsafe { close(client_socket) };
}

// Function to determine content type based on file extension
fn determine_content_type(file_path: &str) -> String {
    match file_path.rsplit('.').next() {
        Some("css") => "text/css".parse().unwrap(),
        Some("txt") => "text/plain".parse().unwrap(),
        Some("js") => "application/javascript".parse().unwrap(),
        Some("png") => "image/png".parse().unwrap(),
        Some("jpg") | Some("jpeg") => "image/jpeg".parse().unwrap(),
        Some("gif") => "image/gif".parse().unwrap(),
        Some("pdf") => "application/pdf".parse().unwrap(),
        Some("htm") | Some("html") => "text/html".parse().unwrap(),
        _ => "application/octet-stream".parse().unwrap(),
    }
}

/// # Url Decode crate wrapper
/// **Crate used**: url decode, but have made the wrapper which suits rohanasan's needs.
///
/// ## Usage:
/// ```ignore
/// decode(request.get_request())
/// ```
/// ## Explanation:
/// - Suppose request.get_request() contained: `q=Hello%20world`
/// - decode will return: `q=Hello world`
pub fn decode(x: &str) -> String {
    urldecode::decode(x.to_string())
}