Hi Gregg, fantastic video. Thank you! one question, in relation to ca. minute 13:44. How is the env executed on the browser when the c file is originally meant to be executed with command line arguments? how does one get the command line argument for free? is there a way to compile a c file as if it had been executed with arguments? I hope I made myself clear. I appreciate it, great content!
What you are seeing at 13:45 was not run in the browser. It was run on a server. The browser merely displays the output of the server. There were no command line arguments, what you see displayed are environment variables. The code to generate that output is seen at 13:38 Hope this helps.
So you have to recompile each time you update HTML? And what about DB connection? Does your lib have it or do you have use curl or something like that?
For your first question: Yes, if you code the whole site in C, you need to re-compile every time you make a change. There is a way around that, you write a program which loads a separate file and put your html into that. I wrote a small program (I called it webby), it loads a file and I can put special tags where I want the code to output a special header, put things in a box, the copyright notice at the end, etc. This program allows me to quickly create a blog post. For your second question: Most DBs have a library for use with C so you can just use that as normal.
You could write the cgi script in such a way that it makes use of inotify or something similar so it knows to reload the template once the template file has been updated. It's not necessary to recompile to update the html just because your main code is written in C. Also, recompiling of this code isn't like recompiling a large app, it takes like 1 second.
mysql/maria has a c api and you can use prepared/parameterized queries to help protect against sql injection (mysql_stmt_prepare). I haven't tried this with a web program though.
Funnily enough, I'm almost done rewriting a Bash CGI script in C. The Bash script works fine but takes 4.5 seconds to generate the entire page. The C program generates the same page in about 0.02 seconds. The task consists of pulling 600 rows from a database table, base64 decoding two fields per row and modifying a third, and presenting the lot as a html table.
Question: Let’s say there is a background cgi task that would take time to finish, but you don’t want client to wait for it. How can you send temporary “In progress” status to client and push “Success” html page when the task is done? I’ve tried forking a child, setting new name to process, detaching the child from parent but apache would still wait for all children to die. Any solution?
@@GreggInkCodes No. Simple setup. An html page with and method post. User submits a video or image and in the background I do media manipulation with ffmpeg and after that is finished I present the modified image or video in different page. Problem is while I am processing in the forked child, and parent process is still stuck at sending an html page. Only when the child has done processing and has exited then only apache actually show a page. You can test it yourself by forking a child, detaching it using setsid, renaming it with , even killing parent with kil(getppid($), and then sleeping or doing a long loop in the child while parent returns an html page.
Send them to a URL with a “job ID” in it. Then use a meta tag to refresh the page once every few seconds, check the job ID statue in RAM or a DB/cache, and send the page back if it’s not done or redirect to the success page.
@@manishnaik2052 Why not do it with Javascript? Have a HTML page with a form that sends the video to another C endpoint. With JS you can call that endpoint and display loading while waiting for the video to come back.
I had less experience then making videos and I didn't make the code as clear as I wish I had. You can get the code from the gitlab link in the description though.
Opening hello.exe in a browser is not going to work. This file is supposed to be run from within a webserver like apache. You could do the following: 1. install apache 2. set up apache so as to run cgi programs 3. copy your hello.exe in the proper director; typically /usr/lib/cgi-bin. I don't know what that would be on windows. 4. run apache 5. use browser to connect to localhost and load the webpage produced by hello.exe I realize above steps are rather vague but I couldn't tell you how to do it on winodws.
The output we care about is going through printf. This is what will be send over the internet to the browser of the user. The returns won't be seen by anyone except by apache (or whatever webserver you happen to use) and it's unlikely apache will do anything useful with that return code.
All code was tested and run on my server prior to making the video to make sure everything worked. I just tested it again to make sure. It is working over here. I cannot tell you why it is not working on your system. Could it be a typo? Did you type in the code yourself or did you download it from my gitlab. Also, bear in mind that your browser could be buffering things.
CGI is almost dead! Perl still used by sysadmins, but Pyton are much better. If you wanted to support web development using C, just checkout modern frameworks like an Crow.
Nope. Write it in rust instead. I get writing it in a lower level language, but if that is the decision one makes, writing it in Rust or C++ is probably better.
Thank you for making a video on this. I sometimes feel like I'm the only other person on the planet who makes web apps in C.
And i am about to attempt to make either:))
Web programming in C. WOW
Future proof it by using a language so old it will probably never go away :)
Ur voice and ur channel ur videos amazing
If I have ur voice I never stop uploading videos
Thank you, nice video
Nice explanation! Thank you!
Hi Gregg, fantastic video. Thank you! one question, in relation to ca. minute 13:44. How is the env executed on the browser when the c file is originally meant to be executed with command line arguments? how does one get the command line argument for free? is there a way to compile a c file as if it had been executed with arguments? I hope I made myself clear. I appreciate it, great content!
...btw I tried a few ways. For example, creating an executable bash script and passing that as the file to be read by the cgi. But no success!
What you are seeing at 13:45 was not run in the browser. It was run on a server. The browser merely displays the output of the server. There were no command line arguments, what you see displayed are environment variables. The code to generate that output is seen at 13:38 Hope this helps.
@@GreggInkCodes all clear, thanks! its working now:)
So you have to recompile each time you update HTML?
And what about DB connection?
Does your lib have it or do you have use curl or something like that?
For your first question: Yes, if you code the whole site in C, you need to re-compile every time you make a change. There is a way around that, you write a program which loads a separate file and put your html into that. I wrote a small program (I called it webby), it loads a file and I can put special tags where I want the code to output a special header, put things in a box, the copyright notice at the end, etc. This program allows me to quickly create a blog post.
For your second question: Most DBs have a library for use with C so you can just use that as normal.
its common to recompile everytime in all languages if you write html or css in language itself , itss not that only in c you have to doo it
You could write the cgi script in such a way that it makes use of inotify or something similar so it knows to reload the template once the template file has been updated. It's not necessary to recompile to update the html just because your main code is written in C. Also, recompiling of this code isn't like recompiling a large app, it takes like 1 second.
@@Miles-co5xm to add to this, even in some live server, refreshing usually have to be done.
:)
mysql/maria has a c api and you can use prepared/parameterized queries to help protect against sql injection (mysql_stmt_prepare). I haven't tried this with a web program though.
I appreciate the effort but I cannot read that text
imagine writing CGI in C, instead of posix shell scripts ;P
Funnily enough, I'm almost done rewriting a Bash CGI script in C. The Bash script works fine but takes 4.5 seconds to generate the entire page. The C program generates the same page in about 0.02 seconds. The task consists of pulling 600 rows from a database table, base64 decoding two fields per row and modifying a third, and presenting the lot as a html table.
Question: Let’s say there is a background cgi task that would take time to finish, but you don’t want client to wait for it.
How can you send temporary “In progress” status to client and push “Success” html page when the task is done?
I’ve tried forking a child, setting new name to process, detaching the child from parent but apache would still wait for all children to die.
Any solution?
Are you using that with XMLHttpRequest() ?
@@GreggInkCodes No. Simple setup. An html page with and method post. User submits a video or image and in the background I do media manipulation with ffmpeg and after that is finished I present the modified image or video in different page. Problem is while I am processing in the forked child, and parent process is still stuck at sending an html page. Only when the child has done processing and has exited then only apache actually show a page.
You can test it yourself by forking a child, detaching it using setsid, renaming it with , even killing parent with kil(getppid($), and then sleeping or doing a long loop in the child while parent returns an html page.
Send them to a URL with a “job ID” in it. Then use a meta tag to refresh the page once every few seconds, check the job ID statue in RAM or a DB/cache, and send the page back if it’s not done or redirect to the success page.
@@manishnaik2052 Why not do it with Javascript? Have a HTML page with a form that sends the video to another C endpoint. With JS you can call that endpoint and display loading while waiting for the video to come back.
Can't see the coding clearly , Even after selecting the highest resolution??
I had less experience then making videos and I didn't make the code as clear as I wish I had. You can get the code from the gitlab link in the description though.
Maybe a bigger terminal font will help :)
Yeah, it has been pointed out to me. I will try to make the font bigger in future videos. Thanks.
What about windows? I get hello.exe output and I'm not really able to open this up in a web browser.
Opening hello.exe in a browser is not going to work. This file is supposed to be run from within a webserver like apache. You could do the following:
1. install apache
2. set up apache so as to run cgi programs
3. copy your hello.exe in the proper director; typically /usr/lib/cgi-bin. I don't know what that would be on windows.
4. run apache
5. use browser to connect to localhost and load the webpage produced by hello.exe
I realize above steps are rather vague but I couldn't tell you how to do it on winodws.
I am asking myself for nearly 40 years: why I am dealing with this? so whatever
kcgi ftw
Bro plz change your theme. Red color isn’t visible enough.
You mean the macros inside gnu nano? Yeah, I didn't realize that at the time but I don't think I will ever use nano for this again in a video. Cheers.
@@GreggInkCodes Appreciate it
And what about returns? You always return 1.
The output we care about is going through printf. This is what will be send over the internet to the browser of the user. The returns won't be seen by anyone except by apache (or whatever webserver you happen to use) and it's unlikely apache will do anything useful with that return code.
@@veic Yeah, returning zero would have been better. That was a mistake.
@@veic c takes more write time, its more complex, and things are easier and faster with domain specific languages.
@@veic i guess without the return zero still will work
Based 🧐
I don't understand that comment.
@@tommyddoom Can you enlighten me so? What does it mean?
hmm ... your header relocation doesnt seem to work for me ... but i tryed just Location: %s
and it worked
All code was tested and run on my server prior to making the video to make sure everything worked. I just tested it again to make sure. It is working over here. I cannot tell you why it is not working on your system. Could it be a typo? Did you type in the code yourself or did you download it from my gitlab. Also, bear in mind that your browser could be buffering things.
CGI is almost dead!
Perl still used by sysadmins, but Pyton are much better.
If you wanted to support web development using C, just checkout modern frameworks like an Crow.
can you please make the text smaller ?
if you have problem with the work of the professor of this lecture - read a book
And is unthinkable to C to disapear (note the sarcasm). Rust will kill C as PHP kill Perl.
Rust might replace C++, but not C.
C is mostly used in embedded systems and safety-critical systems, that's not going to change any time soon.
@@thetastefultoastie6077 I really think that it will replace C is most cases.
People said same with C++ but C is will turn out to be superior
@@dominicamoakobaah9516 This time no.
@@engelsteinberg593 let's see
Nope. Write it in rust instead. I get writing it in a lower level language, but if that is the decision one makes, writing it in Rust or C++ is probably better.
Ew
Not C++, its garbage. Rust is good tho, but it is fairly new.