CGI in C and Dart: Introduction (1)

CGI in C and Dart

Nowadays, when creating a dynamic homepage, "launch a Linux instance with VPS", "deliver content using a Serverless framework", "use K8S etc. to drop it to Container" I think the recent trend is to "play".

However, until recently, it was common to rent a rental server and use CGI to deliver dynamic content. I will explain about this CGI.

Use CGI from C and Dart

Also, speaking of CGI, Perl and PHP are often used, Here, C language and Dart language are targeted.

There was no story I used for CGI in the Dart language, either DuckDuckGo or Google.

Of course, the reason there was none was classic technology, Probably because it was never used in the future

CGI is simple and easy

Is it around 2004-2006? PHP became widespread and many services were created with CGI. Many are non-engineers. Even such non-engineers A service that gives you an overall picture and makes money has been created.

Compared to Serverless such as React Vue Flutter and Firebase Lambda these days I think it was because it was easy to learn.

What is CGI

CGI stands for Common Gateway Interface. https://en.wikipedia.org/wiki/Common_Gateway_Interface https://tools.ietf.org/html/rfc3875

It determines how to interact, especially between the server and an external program. It's super simple unlike the current Interface.

Launch the app from the server and get the execution result

In CGI, the application is started by specifying the parameters from the server. Get the standard output output by the app as the return value.

Let's take a concrete look.

App that displays Hello, World !!

hello.c


// hello.c
// gcc hello.c -o hello.cgi
#include<stdio.h>

int main(int argc, char* argv[]) {
    printf("Content-type: text/html\n\n");
    printf("Hello,World!!\n");
    return 0;
}

Suppose you have an app named hello.cgi. This app, when you enter the command ./hello.cgi, """ Content-type: text/html

Hello,World!! """ And, it is an application that only displays the character string on the console.

The page will display Hello, World !!

If you call "http://example.com/hello.cgi" from your browser, "Hello, World !!" is displayed in the browser.

In this way, call the app from the server CGI can return the output from the application to the user

Let's actually move it

We have prepared a Docker environment. Please refer to the Docker file for detailed settings !!

Start Container

$ git clone https://github.com/kyorohiro/dartlang_cgi.git
$ cd ./dartlang_cgi/001
$ docker-compose build
$ docker-compose up -d

Launch VS Code

Open VSCode in Docker Container in your browser http://127.0.0.1:8443

Launch Apache server in vscode

$ apache2

build cgi

$ cd /app/www/cgi
$ gcc hello.c -o hello.cgi
$ dart2native hello_dart.dart -o hello_daet.cgi

Operation check

When you open http://127.0.0.1:8080/, ./app/www/index.html will be displayed.

When you open http://127.0.0.1:8080/cgi-bin/hello.cgi. ./app/cgi/hello.cgi execution result is returned

Opening http://127.0.0.1:8080/cgi-bin/hello_dart.cgi will return the execution result of ./app/cgi/hello_dart.cgi

Supplement

Dart can be compiled to Native by using dart2native. I'm using it this time.

main(List<String> args) {
  print("Content-type: text/html\n\n");
  print("Hello,World From Dart!!\n");
  return 0;
}

code

https://github.com/kyorohiro/dartlang_cgi

next time

GET, POST, cookies, Let's deal with DB.

Recommended Posts

CGI in C and Dart: Introduction (1)
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Differences in writing Java, C # and Javascript classes
Organize builds in C ++ / Java and Win / Linux combinations
SystemSpec introduction and writing
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Ruby C extension and volatile
Reproduce Java enum in C #
C # and Java Overrides Story
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java