CSP makes it possible to embed C and C++ code in a standard HTML file. The C and C++ code embedded in HTML can communicate directly with your application and/or hardware.
The CSP compiler is designed to be run standalone on a host computer and is usually run via a makefile. Most of our example makefiles use the CSP compiler/linker. The CSP compiler translates a CSP file into a C/C++ file containing one HttpPage. The CSP file is an easier way to create an extended HttpPage. The generated HttpPage is compiled to object code by using your target C or C++ compiler.
The CSP compiler makes it possible for a web designer to design the framework for a page without any knowledge of C or C++, and a C or C++ programmer can later add the dynamic content to the page. If the HTML framework needs to be changed at a later stage, the web designer can use his favorite HTML tool for changing the page. All HTML tools leave unknown HTML tags alone, and the C or C++ code embedded in the page will not be altered.
The features offered by our CSP technology are also similar to that offered by Microsoft's Active Server Pages (ASP), thus ASP programmers should also have an easier time with our scripting environment.
A HttpPage is comparable to a Java Servlet. The CSP compiler translates a CSP file into a HttpPage. A visitor requesting a page makes the Web-Server look up the page using the Virtual file System. When the page is found, the Web-Server calls the page service function which is automatically generated by the CSP compiler for a CSP page.
The above CSP code contains only static HTML elements, and the request object passed as argument to the service function is not used by the above CSP HTML code. The HttpPage generated for the above example writes the static HTML to the response object, but the above example only serves as an illustration on how a CSP page conceptually works. The actual HTML code is not emitted as above, but is fetched from a cspPages.dat file generated by the CspLink tool, which will be explained later.
Though the above example containing only static HTML works, the CSP technology's strength is in web applications that serve dynamic content.
CSP pages are typically comprised of:You can thus create and maintain CSP pages by conventional HTML/XML tools. Here is an example of a simple Web-Counter script that can be compiled by CspCompile:
<%! int myCount; %> <%!! this->myCount = 0; %> <HTML> <BODY> <P> This web page has been visited <%HttpResponse_printf(response, "%d ",this->myCount++);%> number of times </P> </BODY> </HTML>
The CspCompiler recognizes the <% tag as the start of the embedded C code and the %> tag as the end of the embedded C code section.
The CspCompiler can also generate C++ code and the script can therefore be written as:
<%! int myCount; %> <%!! myCount = 0; %> <HTML> <BODY> <P> This web page has been visited <%response->printf("%d ",myCount++);%> number of times </P> </BODY> </HTML>
The above two examples can also be written in C or C++ as:
<%! int myCount; %> <%!! this->myCount = 0; %> <HTML> <BODY> <P> This web page has been visited<%="%d" this->myCount++%>number of times </P> </BODY> </HTML>
A web application is typically comprised of dynamically generated pages (CSP) and static content such as gif images, style sheets, etc. The CspCompiler can also compile static content, but this is inefficient as a HttpPage is generated for each resource. A better approach is to use a HttpResRdr instance together with a ZipIo instance for storing static content. The HttpResRdr together with a ZipIo turns a ZIP file into a read-only file system. The Web-Server combines directories and pages in the ZIP file and CSP pages in the Virtual File System if the directory name is the same. This means that a visitor will view the two directory branches as one. One can thus have some of the pages in the ZIP file and some pages as CSP in the Virtual File System, and these pages will be combined into one at runtime.
The Web Server also comes with a tool that simplifies the integration of dynamic/static HTML into your system. The CspMakeGen Lua script can be used for generating a makefile for your HTML. The tool can recursively traverse a directory containing HTML files. The tool will generate a makefile dependency for all .html and .shtml found in the directory and sub directories.
The above has served as a quick introduction to our CSP technology. Any ASP or JSP programmer should be able to quickly become familiar with our CSP technology.
The CspCompiler recognizes 7 different tags.
The CSP tags:
<%g %> |
Global declarations. Things like #include directives and declaration of types. |
<%! %> |
Declarations. An HttpPage object can add its own data members. Note that you cannot declare and initialize at the same time. You initialize the variable with the !! tag. |
<%!! %> |
Initialization. The HttpPage constructor code goes here. This method is called once when application is started and should be used to initialize the variables declared with the ! tag. |
<%p %> |
prologue code. Provides a convenient way of making sure that whatever goes here is emitted at the top of HttpPage_service, the CSP service function. This tag is useful if you write C code since it makes it possible to declare variables in the scope of the CSP service function. C++ code is not limited to declaring variables at the top of a scope declaration. |
<%e %> |
Epilogue code. Cleanup code that must be run just before the HttpPage_service function returns. The compiler emits a label just before the epilogue code. The generated code jumps to this label if any error is detected during execution. You can also use this label in your code as a way to emulate exceptions. Example: if(error) goto L_epilogue; |
<% %> |
Code fragments. A code fragment can contain any number of language statements, variable declarations, or expressions that are valid in the page scripting language, which is C or C++. |
<%= %> |
The expression tag. The format flags are the same as the ones used by printf. If you do not specify a format flag, the expression is assumed to be a string, i.e. the two following tags will produce the same result: |
The HttpPage class contains the Web Page Service Function, which is executed by the Virtual File System when the resource is accessed by a client. The Web Page Service Function provides similar functionality as the servlet service function, in a Java HTTP Servlet application server.
A user requesting the following URL, http://myServer/dir1/dir2/myPage.html, makes the Web Server traverse the Virtual File System root->dir1->dir2. The dir2 HttpDir instance locates myPage.html page and calls the Page Service Function.
The HttpPage is an abstract class that must be overloaded:
C header file example:struct MyPage { HttpPage super; /* As if inherited */ int myCount; };C++ header file example:
class MyPage : public HttpPage { int myCount; };
The page object must provide two functions: a constructor and the service function.
C code example:static void MyPage_service(struct HttpPage* page, HttpRequest* request, HttpResponse* response) { MyPage* o = (MyPage*)page; /* Cast to my object */ HttpResponse_printf(response, "<html><body>"); HttpResponse_printf(response, "<p>This web page has been visited %d number of times</p>", o->myCount); HttpResponse_printf(response, "</body></html>"); } void MyPage_constructor(MyPage* o) { HttpPage_constructor(o, MyPage_service, "MyPage"); o->myCount = 0; }
The CspCompiler generates the necessary code for the HttpPage object. Using the CspCompiler instead of manually writing the HttpPage service function is much easier. A manually written service function should only be used if there are little or no static HTML elements in the HTML page.
See CSP Tools for more information on the CspCompiler and the tools required for compiling CSP pages.
Server Side Include Files, which should not be confused with server side scripting, are special files that are invisible for regular browser requests. Server Side Include Files, which should have the ".shtml" ending, are recognized by the CspCompiler as files that can only be accessed by method HttpResponse::include and HttpResponse::forward. A direct request from a browser yields "404 not found" in the response.