Apache Module: mod_randomint

Small Apache module to generate random integers
  1. #include "httpd.h"
  2. #include "http_config.h"
  3. #include "http_core.h"
  4. #include "http_log.h"
  5. #include "http_protocol.h"
  6.  
  7. #define MAX_COUNT 128
  8. #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
  9.  
  10. static int chunk_reader(request_rec *r, const char **rbuf)
  11. {
  12. int ret;
  13.  
  14. if(OK != (ret = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR))) {
  15. return ret;
  16. }
  17.  
  18. if(ap_should_client_block(r)) {
  19. char argsbuffer[HUGE_STRING_LEN];
  20. int rsize, len_read, rpos=0;
  21. long length = r->remaining;
  22.  
  23. *rbuf = apr_pcalloc(r->pool, length + 1);
  24.  
  25. while(0 < (len_read = ap_get_client_block(r, argsbuffer, sizeof(argsbuffer)))) {
  26. if ((rpos + len_read) > length) {
  27. rsize = length - rpos;
  28. }
  29. else {
  30. rsize = len_read;
  31. }
  32.  
  33. memcpy((char*)*rbuf + rpos, argsbuffer, rsize);
  34. rpos += rsize;
  35. }
  36. }
  37.  
  38. return ret;
  39. }
  40.  
  41. static int read_post(request_rec *r, apr_table_t **tab)
  42. {
  43. const char *data;
  44. const char *key, *val, *type;
  45. int ret = OK;
  46.  
  47. if(r->method_number != M_POST) {
  48. return ret;
  49. }
  50.  
  51. type = apr_table_get(r->headers_in, "Content-Type");
  52.  
  53. if(strcasecmp(type, DEFAULT_ENCTYPE) != 0) {
  54. return DECLINED;
  55. }
  56.  
  57. if((ret = chunk_reader(r, &data)) != OK) {
  58. return ret;
  59. }
  60.  
  61. if(*tab) {
  62. apr_table_clear(*tab);
  63. }
  64. else {
  65. *tab = apr_table_make(r->pool, 8);
  66. }
  67.  
  68. while(*data && (val = ap_getword(r->pool, &data, '&'))) {
  69. key = ap_getword(r->pool, &val, '=');
  70. ap_unescape_url((char*)key);
  71. ap_unescape_url((char*)val);
  72. apr_table_merge(*tab, key, val);
  73. }
  74.  
  75. return OK;
  76. }
  77.  
  78. int get_value(request_rec *r, const char *name)
  79. {
  80. apr_table_t *post_values = NULL;
  81. apr_array_header_t *arr;
  82. apr_table_entry_t *elt;
  83.  
  84. int i, ret, value;
  85.  
  86. if(OK != (ret = read_post(r, &post_values))) {
  87. return -1;
  88. }
  89.  
  90. if(NULL == post_values) {
  91. return -1;
  92. }
  93.  
  94. arr = apr_table_elts(post_values);
  95. elt = (apr_table_entry_t*)arr->elts;
  96.  
  97. for(i = 0; i < arr->nelts; ++i) {
  98. if(strcmp(elt[i].key, name) == 0
  99. && LONG_MIN != (value = strtol(elt[i].val, NULL, 10))) {
  100. return value;
  101. }
  102. }
  103.  
  104. return -1;
  105. }
  106.  
  107. static int mod_randomint_method_handler (request_rec *r)
  108. {
  109. int ret = OK, i, count = 8, new_count;
  110. unsigned int random_value;
  111.  
  112. if(strcmp(r->handler, "randomint-handler")) {
  113. return DECLINED;
  114. }
  115.  
  116. new_count = get_value(r, "count");
  117.  
  118. if(new_count > 0 && new_count < MAX_COUNT) {
  119. count = new_count;
  120. }
  121.  
  122. ap_set_content_type(r, "text/plain");
  123.  
  124. int random_fd = open("/dev/urandom", O_RDONLY);
  125. for(i = 0; i < count; i ++) {
  126. read(random_fd, (void*)&random_value, sizeof(random_value));
  127. ap_rprintf(r, "%u\n", random_value);
  128. }
  129. close(random_fd);
  130.  
  131. // fprintf(stderr, "Count: %d\n", count);
  132. // fflush(stderr);
  133.  
  134. return ret;
  135. }
  136.  
  137. static void mod_randomint_register_hooks (apr_pool_t *p)
  138. {
  139. ap_hook_handler(mod_randomint_method_handler, NULL, NULL,APR_HOOK_LAST);
  140. }
  141.  
  142. module AP_MODULE_DECLARE_DATA randomint_module =
  143. {
  144. STANDARD20_MODULE_STUFF,
  145. NULL,
  146. NULL,
  147. NULL,
  148. NULL,
  149. NULL,
  150. mod_randomint_register_hooks, /* callback for registering hooks */
  151. };
  152.  
Permalink  Download