nginx字符串的数据类型的表示
nginx对c语言的字符串类型进行了简单的封装, core/ngx_string.h/c 里面包含这些封装的内容
其中定义了 ngx_str_t ,ngx_keyval_t, ngx_variable_value_t
这几个基础类型的定义如下
- typedef struct {
- size_t len;
- u_char *data;
- } ngx_str_t;
- typedef struct {
- ngx_str_t key;
- ngx_str_t value;
- } ngx_keyval_t;
- typedef struct {
- unsigned len:28;
- unsigned valid:1;
- unsigned no_cacheable:1;
- unsigned not_found:1;
- unsigned escape:1;
- u_char *data;
- } ngx_variable_value_t;
可以看出 ngx_str_t 在原有的uchar* 的基础上加入的字符串长度的附加信息, 初始化使用ngx_string宏进行,他的定义为:
- #define ngx_string(str) { sizeof(str) - 1, (u_char *) str }
测试字符串的代码 demo/basic_types/basic_types_str.c
- #include
- #include "ngx_config.h"
- #include "ngx_conf_file.h"
- #include "nginx.h"
- #include "ngx_core.h"
- #include "ngx_string.h"
- #include "ngx_string.h"
- volatile ngx_cycle_t *ngx_cycle;
- void
- ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
- const char *fmt, ...)
- {
- }
- int main()
- {
- u_char* p = NULL;
- ngx_uint_t size;
- ngx_str_t dst;
- ngx_str_t mystr = ngx_string("hello, world !");
- ngx_keyval_t pair = {ngx_string("url"), ngx_string("")};
- int dst_len =ngx_base64_encoded_length(mystr.len);
- printf("source length is %d, destination length is %d\n", mystr.len, dst_len );
- p = malloc( ngx_base64_encoded_length(mystr.len) + 1);
- dst.data = p;
- ngx_encode_base64(&dst, &mystr);
- printf("source str is %s\ndestination str is %s\n", mystr.data, dst.data);
- free(p);
- size = pair.value.len + 2 * ngx_escape_uri(NULL, pair.value.data, pair.value.len, NGX_ESCAPE_URI);
- p = malloc (size * sizeof(u_char));
- ngx_escape_uri(p, pair.value.data, pair.value.len, NGX_ESCAPE_URI);
- printf("escaped %s is : %s (%d)\noriginal url size is %d\n", pair.key.data, p, size, pair.value.len);
- free(p);
- return 0;
- }
编译运行
- gcc -c -O -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I ../../../objs/ -I ../../os/unix/ basic_types_str.c -I../../core/ -I../../event/ -I../../os/ -o basic_types_str.o
- gcc -o basic_types_str basic_types_str.o ../../../objs/src/core/ngx_{string,palloc}.o ../../../objs/src/os/unix/ngx_alloc.o -lcrypt -lpcre -lz
- ./basic_types_str
ubuntu的测试环境 我去掉了参数 -lcrypto
测试结果:
- source length is 14, destination length is 20
- source str is hello, world !
- destination str is aGVsbG8sIHdvcmxkICE=
- escaped url is : %3ftest=1 (34)
- original url size is 32
core/ngx_string.h/c 中同时也封装了一批字符/字符串处理的函数和宏,他们的使用大多数情况下和c标准库中的类似,只是在内存分配相关的函数中有一定的区别。
比如 u_char *ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src); 除了源字符串驻外,还要传入ngx_pool_t的指针作为参数,使用nginx自己的内存分配方式进行内存的分配。
除了标准的字符串操作外, nginx还实现了例如:
- // base64 编码/解码函数和宏
- #define ngx_base64_encoded_length(len) (((len + 2) / 3) * 4)
- #define ngx_base64_decoded_length(len) (((len + 3) / 4) * 3)
- void ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src);
- ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src);
- //utf-8 编码/解码相关函数
- uint32_t ngx_utf8_decode(u_char **p, size_t n);
- size_t ngx_utf8_length(u_char *p, size_t n);
- u_char *ngx_utf8_cpystrn(u_char *dst, u_char *src, size_t n, size_t len);
- // urlencode和html实体的编码解码
- uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size,
- ngx_uint_t type);
- void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type);
- uintptr_t ngx_escape_html(u_char *dst, u_char *src, size_t size);
等对于http服务有帮助的宏和函数
阅读(5841) | 评论(0) | 转发(0) |