Apache MOD Development STEP 2 Get User Enter

xiaoxiao2021-03-06  43

Apache MOD Development STEP 2 Get User Enter

1. Understand the APR_TABLE_T structure

You can understand him as a Hash table, you can assign a value to him, commonly used

APR_TABLE_ADD

APR_TABLE_SET

APR_TABLE_GET

E.g:

Char * Slen = APR_TABLE_GET (r-> Headers_IN, "Content-Length");

2. Learn about REQUEST_REC

This is the most important structure defined in httpd.h. 682.

Notice the unique parameters of the Handle function

Static int mod_hello_method_handler (Request_rec * R); you can think that you can get everything from this structure.

Important couple

APR_POOL_T * POOL;

/ ** the connection to the client * /

CONN_REC * Connection;

/ ** The Virtual Host for this request * /

Server_rec * Server;

INTMETHOD_NUMBER; / / Submit information type, get or pos

Char * args; // Store Get parameters

APR_TABLE_T * Headers_IN; / / Save the information of the header information

Const char * handler; // Type of processing

3. Read http header

Information is in r-> headers_in, then

Char * Slen = APR_TABLE_GET (r-> Headers_IN, "Content-Length");

4. Get data passed in GET method

Information is in r-> args. Note that the data here is not parsed, that is, the URL encoded, if you don't use it like libaPReq2, you need yourself to encode.

AP_LOG_RERROR (APLOG_MARK, APLOG_ERR, 0, R, "Get Query String:% S", R-> ARGS;

5. Get data transmitted by the POST method

Data In the bucket associated with Request_Rec. The interpretation of Bucket will explain in the next step, then we simply use the AP_GET_CLIENT_BLOCK to read it. In fact, this function is also called BUCKET operation.

6. Simple example.

IF ("Hello-script", r-> handler) Return Declined;

// Get the comand.

IF (r-> method_number == m_get) {

AP_LOG_RERROR (APLOG_MARK, APLOG_ERR, 0, R, "Get Query String:% S", R-> ARGS;

} else if (r-> method_number == m_post) {

Handle_post (r);

} else {

Return Declined;

}

Handle_post function

Void Handle_Post (Request_Rec * R)

{

SIZE_T TOTAL_BYTES;

INT RSTAT = 0;

Char cbuf [huge_string_len];

Rstat = AP_SETUP_CLIENT_BLOCK (R, Request_chunked_Dechunk);

IF (AP_SHOULD_CLIENT_BLOCK (R)) {

Int nbytes;

While ((nbytes = ap_get_client_block (r, cbuf, huge_string_len)> 0) {

CBUF [NBYTES] = '/ 0'; AP_RPUTS (CBUF, R);

Total_Bytes = nbytes;

}

}

}

7. Small knot

Now there is a simple understanding of the module, know how to write a module, know where the data is to process the input and output, let's know how the APAHCE is operating, the common function of the APCHE, then uses Apache's service framework Complete more work. To take Apache to a Socket server, not just a web server.

转载请注明原文地址:https://www.9cbs.com/read-59594.html

New Post(0)