Web programming (CGI) in C

Поделиться
HTML-код
  • Опубликовано: 8 фев 2025

Комментарии • 99

  • @BorlandC452
    @BorlandC452 2 года назад +4

    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.

    • @alexbright9849
      @alexbright9849 2 года назад

      And i am about to attempt to make either:))

  • @guckesksk5895
    @guckesksk5895 4 года назад +20

    Web programming in C. WOW

  • @billowen3285
    @billowen3285 4 года назад +6

    Future proof it by using a language so old it will probably never go away :)

  • @Awwe12675
    @Awwe12675 2 года назад +2

    Ur voice and ur channel ur videos amazing
    If I have ur voice I never stop uploading videos

  • @alexdzyuba5123
    @alexdzyuba5123 3 года назад +2

    Thank you, nice video

  • @ТарааХовалыг
    @ТарааХовалыг 3 года назад

    Nice explanation! Thank you!

  • @guillemtz5079
    @guillemtz5079 Год назад +1

    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!

    • @guillemtz5079
      @guillemtz5079 Год назад

      ...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!

    • @GreggInkCodes
      @GreggInkCodes  Год назад +1

      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.

    • @guillemtz5079
      @guillemtz5079 Год назад

      @@GreggInkCodes all clear, thanks! its working now:)

  • @weeb3277
    @weeb3277 3 года назад +4

    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?

    • @GreggInkCodes
      @GreggInkCodes  3 года назад +4

      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.

    • @Miles-co5xm
      @Miles-co5xm 3 года назад +3

      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

    • @nexusclarum8000
      @nexusclarum8000 3 года назад

      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.

    • @samsonmayeem8409
      @samsonmayeem8409 3 года назад

      @@Miles-co5xm to add to this, even in some live server, refreshing usually have to be done.
      :)

    • @ironfist7789
      @ironfist7789 Год назад

      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.

  • @cambium0
    @cambium0 Год назад +1

    I appreciate the effort but I cannot read that text

  • @jan_harald
    @jan_harald 3 года назад +6

    imagine writing CGI in C, instead of posix shell scripts ;P

    • @snowbatsnowbat7197
      @snowbatsnowbat7197 3 года назад +2

      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.

  • @manishnaik2052
    @manishnaik2052 2 года назад +2

    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
      @GreggInkCodes  2 года назад +1

      Are you using that with XMLHttpRequest() ?

    • @manishnaik2052
      @manishnaik2052 2 года назад +2

      @@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.

    • @vidyagametourist
      @vidyagametourist Год назад +1

      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.

    • @sawyer303
      @sawyer303 Год назад +1

      @@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.

  • @davscotwar
    @davscotwar 2 года назад +2

    Can't see the coding clearly , Even after selecting the highest resolution??

    • @GreggInkCodes
      @GreggInkCodes  2 года назад +1

      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.

  • @soham7510
    @soham7510 3 года назад +1

    Maybe a bigger terminal font will help :)

    • @GreggInkCodes
      @GreggInkCodes  3 года назад

      Yeah, it has been pointed out to me. I will try to make the font bigger in future videos. Thanks.

  • @Maraus92
    @Maraus92 3 года назад +1

    What about windows? I get hello.exe output and I'm not really able to open this up in a web browser.

    • @GreggInkCodes
      @GreggInkCodes  3 года назад +5

      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.

  • @elalemanpaisa
    @elalemanpaisa 5 месяцев назад

    I am asking myself for nearly 40 years: why I am dealing with this? so whatever

  • @openmarkand
    @openmarkand 2 года назад +1

    kcgi ftw

  • @JR_hSN
    @JR_hSN 2 года назад +2

    Bro plz change your theme. Red color isn’t visible enough.

    • @GreggInkCodes
      @GreggInkCodes  2 года назад +2

      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.

    • @JR_hSN
      @JR_hSN 2 года назад

      @@GreggInkCodes Appreciate it

  • @Maraus92
    @Maraus92 3 года назад +1

    And what about returns? You always return 1.

    • @GreggInkCodes
      @GreggInkCodes  3 года назад +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.

    • @GreggInkCodes
      @GreggInkCodes  3 года назад +2

      @@veic Yeah, returning zero would have been better. That was a mistake.

    • @JstJaybeingJay
      @JstJaybeingJay 3 года назад

      @@veic c takes more write time, its more complex, and things are easier and faster with domain specific languages.

    • @samsonmayeem8409
      @samsonmayeem8409 3 года назад

      @@veic i guess without the return zero still will work

  • @gustavojoaquin_arch
    @gustavojoaquin_arch 2 года назад +4

    Based 🧐

    • @GreggInkCodes
      @GreggInkCodes  2 года назад

      I don't understand that comment.

    • @GreggInkCodes
      @GreggInkCodes  2 года назад

      @@tommyddoom Can you enlighten me so? What does it mean?

  • @pancelalkov2070
    @pancelalkov2070 Год назад +1

    hmm ... your header relocation doesnt seem to work for me ... but i tryed just Location: %s

    and it worked

    • @GreggInkCodes
      @GreggInkCodes  Год назад +1

      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.

  • @NatalijaChugreeva
    @NatalijaChugreeva 3 месяца назад

    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.

  • @cr4zyg3n36
    @cr4zyg3n36 Год назад +1

    can you please make the text smaller ?

    • @elalemanpaisa
      @elalemanpaisa 5 месяцев назад

      if you have problem with the work of the professor of this lecture - read a book

  • @engelsteinberg593
    @engelsteinberg593 3 года назад +2

    And is unthinkable to C to disapear (note the sarcasm). Rust will kill C as PHP kill Perl.

    • @thetastefultoastie6077
      @thetastefultoastie6077 3 года назад +2

      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.

    • @engelsteinberg593
      @engelsteinberg593 3 года назад

      @@thetastefultoastie6077 I really think that it will replace C is most cases.

    • @dominicamoakobaah9516
      @dominicamoakobaah9516 3 года назад +1

      People said same with C++ but C is will turn out to be superior

    • @engelsteinberg593
      @engelsteinberg593 3 года назад

      @@dominicamoakobaah9516 This time no.

    • @dominicamoakobaah9516
      @dominicamoakobaah9516 3 года назад +1

      @@engelsteinberg593 let's see

  • @simonfarre4907
    @simonfarre4907 3 года назад +1

    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.