ngx_tcc_module-20110720 (written in C back-page script)

2011-08-01  来源:本站原创  分类:Internet  人气:107 

ngx_tcc_module an nginx http module, its the tcc compiler embedded in nginx, written in C language used to compile the script page background.

Each page consists of two files:

*. Csp, is mainly written in HTML files, which will be embedded in some C code.

*. C, written in pure C code files, which are mainly to do some business logic processing.

Download:

https://github.com/downloads/hehaiqiang/ngwsx/ngx_tcc_module-20110720.rar

Code Example:

index.csp

<%
/* This is a "c server page (CSP)" */
static int  i;
%>
<html>
<head>
</head>
<body>
<% for (i = 0; i < 5; i++) { %>
<a href="<%=url%>" target="_blank"><%=title%></a>
<% } %>
</body>
</html>

index.c

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

#define NGX_DBD_OPT_NON_BLOCKING  0x01

static void row_handler(ngx_http_request_t *r);

static void    *dbd;
static u_char  *url = "";
static u_char  *title = "";

static void
finalize(ngx_http_request_t *r, ngx_int_t rc)
{
    ngx_tcc_dbd_close(dbd);
    ngx_tcc_dbd_destroy(dbd);

    ngx_tcc_finalize(r, rc);
}

static void
field_handler(ngx_http_request_t *r)
{
    off_t       offset;
    size_t      size, total;
    u_char     *value;
    ngx_int_t   rc;

    for ( ;; ) {

        rc = ngx_tcc_dbd_field_read(dbd, &value, &offset, &size, &total);

        if (rc == NGX_AGAIN) {
            return;
        }

        if (rc == NGX_ERROR) {
            finalize(r, rc);
            return;
        }

        if (rc == NGX_DONE) {
            break;
        }

        /* TODO: value, offset, size, total */

        ngx_tcc_output(r, value, size);
        ngx_tcc_output_str(r, "&nbsp;&nbsp;");
    }

    ngx_tcc_output_str(r, "<br/>");

    ngx_tcc_dbd_set_handler(dbd, row_handler, r);
    row_handler(r);
}

static void
row_handler(ngx_http_request_t *r)
{
    ngx_int_t  rc;

    for ( ;; ) {

        rc = ngx_tcc_dbd_row_read(dbd);

        if (rc == NGX_AGAIN) {
            return;
        }

        if (rc == NGX_ERROR) {
            finalize(r, rc);
            return;
        }

        if (rc == NGX_DONE) {
            break;
        }

        ngx_tcc_dbd_set_handler(dbd, field_handler, r);
        field_handler(r);

        return;
    }

    url = "http://www.126.com/";
    title = "126.com";

    finalize(r, NGX_OK);
}

static void
column_handler(ngx_http_request_t *r)
{
    size_t      size;
    u_char     *buf;
    ngx_int_t   rc;

    for ( ;; ) {

        rc = ngx_tcc_dbd_column_read(dbd);

        if (rc == NGX_AGAIN) {
            return;
        }

        if (rc == NGX_ERROR) {
            finalize(r, rc);
            return;
        }

        if (rc == NGX_DONE) {
            break;
        }

        buf = (u_char *) ngx_tcc_dbd_column_name(dbd);
        size = ngx_strlen(buf);
        ngx_tcc_output(r, buf, size);
        ngx_tcc_output_str(r, "&nbsp;&nbsp;");
    }

    ngx_tcc_output_str(r, "<br/>");

    ngx_tcc_dbd_set_handler(dbd, row_handler, r);
    row_handler(r);
}

static void
result_handler(ngx_http_request_t *r)
{
    ngx_int_t  rc;

    rc = ngx_tcc_dbd_query(dbd);

    if (rc == NGX_AGAIN) {
        return;
    }

    if (rc == NGX_ERROR) {
        finalize(r, rc);
        return;
    }

    ngx_tcc_dbd_set_handler(dbd, column_handler, r);
    column_handler(r);
}

static void
connect_handler(ngx_http_request_t *r)
{
    ngx_int_t  rc;

    rc = ngx_tcc_dbd_connect(dbd);

    if (rc == NGX_AGAIN) {
        return;
    }

    if (rc == NGX_ERROR) {
        ngx_tcc_dbd_destroy(dbd);
        ngx_tcc_finalize(r);
        return;
    }

    /* TODO: escape sql */

    ngx_tcc_dbd_set_sql(dbd, "show databases", sizeof("show databases") - 1);

    ngx_tcc_dbd_set_handler(dbd, result_handler, r);
    result_handler(r);
}

__declspec(dllexport) void
handler(ngx_http_request_t *r)
{
    ngx_int_t  rc;

    dbd = (void *) ngx_tcc_dbd_create(r, "libdrizzle");

    ngx_tcc_dbd_set_options(dbd, NGX_DBD_OPT_NON_BLOCKING);
    ngx_tcc_dbd_set_tcp(dbd, "127.0.0.1", 3306);
    ngx_tcc_dbd_set_db(dbd, "mysql");
    ngx_tcc_dbd_set_auth(dbd, "root", "123456");

    ngx_tcc_dbd_set_handler(dbd, connect_handler, r);
    connect_handler(r);
}
相关文章
  • Recommendation 9 great charting page script JavaScript framework 2010-08-20

    1. flot Flot charting framework is based on pure jQuery Javascript framework. It can immediately draw in the client and generate a variety of graphics. Important is it easy to use (there are many optional settings), attractive visual effects and inte

  • Page script and verify the page's input text box 2010-05-13

    <% @ Page contentType = "text / html; charset = UTF-8"%> <% @ Include file = "/ common / taglibs.jsp"%> <a href="javascript:jumpPage(1)"> Home </ a> <s:if test="%{#request.page.pageNo> 1) &q

  • web2.0 jquery plug-in sub-grid page script can be used to refresh the page without ajax 2010-11-10

    displaynum, displaylastNum free to customize the display of page number Page generated js feet ****** ****** **** No sword (2008-03-05 )**** Date Modified :2008-3-12 Add two parameters: displaynum, displaylastNum free to customize the display of page

  • wget to download page script 2011-04-27

    First, the configuration file: [[email protected] gather]$ cat config start=359822 end=550000 threads=15 output=/home/wusx/gather/output log=/home/wusx/gather/_wget.log Then run the script: [[email protected] gather]$ cat wget.sh #!bin/sh # Download tasks

  • Pop-up box before leaving the page 2010-03-27

    Leave the page to check is to use the onbeforeunload event, when the event is declared: Code <Body onbeforeunload = "return pageBeforeunload (event);"> Pm (note the return of a function), the page will pop up from the default asked to conf

  • JS page refresh Summary 2010-04-02

    Realization of automatic refresh page summary: 1) <meta http-equiv="refresh"content="10;url= The jump page "> 10 indicated that 10 seconds refresh interval 2) <script language=''javascript''> window.location.reload (true);

  • Js page Jump (change) 2010-04-19

    Common function toUrl(ul){ if("" == ul || null==ul){ alert(" Thanks for the concern "); }else{ window.open(ul);// This is a new window // window.location.href=ul;// This is directly on the current page jump } return; } Other online: (w

  • Script debugging tool compatible with all browsers firebug 2010-09-03

    Firebug is a plugin under firefox, the most attracted me is that javascript debugging, set a breakpoint control variables is very easy to use, but also in the console write scripts to dynamically modify the page (such as the test data is written page

  • Ajax loaded page executive summary of js 2010-12-01

    Event Background One page on multiple pages, the public call, which involved some js has been written in the public page, the page is loaded by ajax which can not be performed after the js. Solutions 1. With additional means to perform an iframe js,

  • js pop-up page Daquan [transfer] 2010-12-12

    [1, the basic code blocks pop-up window In fact, the code is very simple: <script language="javascript"> window.open ('page.html') </ Script> Because the code is a javascripts, so they should be placed <script language="javas

  • Page bombs java window function 2010-12-31

    1, the basic pop-up window [code] ---------------------------------------- ---------------------------------------- In fact, the code is very simple: <SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.HTML') --> </SCRIPT> B

  • Client page of the character set of the session 2011-05-23

    session is to use the service side, with the character set the client how the page will be something to do with it? This number will be really confusing. However, if the works from the session to understand the words, it is easy to understand. Works

  • The next page to load FF analysis tools 2009-05-10

    Colleagues recommend this as a page is loaded YSlow analysis instrument, as a supplementary httpwatch you ~ From: http://www.blueidea.com/tech/web/2008/6133.asp YSlow are the United States developed a yahoo page score plug-in, the bar is, from which

  • static javascript page value of three methods 2009-08-24

    static javascript page of the three methods of value 2008-06-08 10:31 A: JavaScript delivery of static pages URL value can have URL for articles by value. To take the message to the URL on. Post.htm Copy the contents to the clipboard code: <input typ

  • Refresh page summary of js 2009-08-27

    2) <script> window.location.reload (true); </ script> If you want to refresh a change iframe to the frame put window's name or ID No. 3) <script> window.navigate ( "This page url"); </ script> 4> function abc () ( wind

  • Display Tag extensions - input box jump page 2008-07-07

    Display Tag is really impossible for me bruised and battered. . . Be displaytag.properties configuration file to replace the following three: paging.banner.full: <span>[<a href="{1}"> Home </a>/<a href="{2}"> On

  • Open the page a new window 2008-06-30

    Preface: the project called for a single order to re-open the window and perform printing functions, I have way to open the page to the search function a bit, leaving for future reference. Window.Open Xiangjie 1, window.open () support environment: J

  • ajax using dwr tools to transfer value from the front page to back Java objects inside (back Qinbo personal summary) 2010-03-18

    1.java class public class DwrTest4 (public void addOrUpdateHuiqinbo (int userId, int sn, boolean yes) (System.out.println ("userId =" + userId + ", sn =" + sn + ", yes =" + yes); )) 2.dwr.xml <dwr> <allow> <cre

  • ajax using dwr tools to pass the value from the front page to back Java objects inside, and then according to the results of the object to return to the page up (back Qinbo personal summary) 2010-03-18

    ajax using dwr tools, from the front page the value passed to the Java objects back inside, then according to the results of the object to return to the page up (back Qinbo personal summary) Edit Article Categories: Web front-end 1.java class public

  • ASP.NET page refresh to achieve Methods 2010-02-26

    Look at the ASP.NET page to refresh the realization: First: private void Button1_Click( object sender, System.EventArgs e ) { Response.Redirect( Request.Url.ToString( ) ); } Second: private void Button2_Click( object sender, System.EventArgs e ) { Re