Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:18:56+00:00 2026-05-13T11:18:56+00:00

I have an idea for a project based on the Apache module API. So,

  • 0

I have an idea for a project based on the Apache module API. So, I am writing my first Apache module to learn the API- just a simple hostname lookup:

URL:  http://localhost/hostname/stackoverflow.com
Response data:
Host: stackoverflow.com
Address: 69.59.196.211
Address type: AF_INET

When the request does not include a hostname after the handler path, I just want the Response data to say:

“Hostname not found in request.”

All of this is working, except one thing: the error response keeps getting appended to on subsequent requests that produce the error result. This does not happen with the hostname lookup result (when the hostname is provided). Example of the issue:

Request 1:  http://192.168.1.3/hostname
Response data:
Hostname not found in request.

Request 2:  http://192.168.1.3/hostname
Response data:
Hostname not found in request.
Hostname not found in request.

Request 3:  http://192.168.1.3/hostname
Response data:
Hostname not found in request.
Hostname not found in request.
Hostname not found in request.

I’m sure I’ve done something wrong with my output buffer in the error case – anyone able to point out the problem? Here is the code:

#include <sys/types.h>
#include <netdb.h>
#include <stdio.h>
#include <stdarg.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "httpd.h"
#include "http_log.h"
#include "http_config.h"


static void log_info(request_rec *r, const char *fmt, ...){
    char log_msg[100];
    memset(log_msg,100,0x00);
    va_list args;
    va_start(args,fmt);
    vsprintf(log_msg,fmt,args);
    va_end(args);
    ap_log_error(APLOG_MARK,APLOG_INFO,APR_SUCCESS,r->server,log_msg,NULL);
}
static void log_err(request_rec *r, const char *fmt, ...){
    char log_msg[100];
    memset(log_msg,100,0x00);
    va_list args;
    va_start(args,fmt);
    vsprintf(log_msg,fmt,args);
    va_end(args);
    ap_log_error(APLOG_MARK,APLOG_ERR,APR_SUCCESS,r->server,log_msg,NULL);
}

int count_chrinstr(const char *haystack, const char needle){
    int n=0;
    char *next;
    while( (next=strchr(haystack,needle)) != NULL ){
        haystack=next+1;
        n++;
    }

    return n;
}
void parse_uri(char *uri, char *pieces[]){
    int i=0;
    char *next_tok=strtok(uri,"/");
    while( next_tok != NULL ){
        pieces[i]=next_tok;
        i++;
        next_tok=strtok(NULL,"/");
    }
}

void lookup_hostname(request_rec *r, char *output){
    int num_parts=count_chrinstr(r->uri,'/');
    log_info(r,"Number of parts: %d",num_parts);
    if(num_parts<2){
        log_err(r,"Hostname not found in request, exiting.",NULL);
        strcat(output,"Hostname not found in request.<br>\n");
        return;
    }

    char *pieces[num_parts];
    parse_uri(r->uri,pieces);
    char *host_entered=pieces[1];

    struct hostent *h=gethostbyname(host_entered);

    //host
    output += sprintf(output,"Host: %s<br>\n", h->h_name);

    //aliases
    int i=0;
    while(h->h_aliases[i] != NULL){
        output += sprintf(output,"Alias: %s<br>\n",h->h_aliases[i]);
        i++;
    }

    //addresses
    i=0;
    while(h->h_addr_list[i] != NULL){
        char *net_addr=inet_ntoa( *(struct in_addr*)(h->h_addr_list[i]));
        output += sprintf(output,"Address: %s<br>\n",net_addr);
        i++;
    }
    log_info(r,"Added addresses to output.",NULL);

    //address type
    if(h->h_addrtype != NULL){
        switch(h->h_addrtype){
            case 2:
                strcat(output,"Address type: AF_INET<br>\n");
                break;
            case 10:
                strcat(output,"Address type: AF_INET6<br>\n");
                break;
            default:
                strcat(output,"Address type: Unknown<br>\n");
                break;
        }
    }

}

static int hostname_handler(request_rec *r) {
  if (!r->handler || strcasecmp(r->handler, "hostname") != 0) {
    return DECLINED;
  }

  if (r->method_number != M_GET) {
    return HTTP_METHOD_NOT_ALLOWED;
  }

  char result[10000];
  memset(result,10000,0x00);

  log_info(r,"Starting hostname lookup.",NULL);
  lookup_hostname(r,result);
  ap_set_content_type(r, "text/html");
  ap_rputs(result, r);
  ap_finalize_request_protocol(r);

  return OK;
}


static void hostname_hooks(apr_pool_t *pool) {
  ap_hook_handler(hostname_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA hostname_module = {
  STANDARD20_MODULE_STUFF,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  hostname_hooks
};

Thanks in advance,

-aj

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:18:56+00:00Added an answer on May 13, 2026 at 11:18 am

    You have the parameters to memset around the wrong way:

    memset(result,10000,0x00);
    

    That line uses a count of zero, which means it does nothing. Instead of using memset, you could just do this:

    char result[10000] = { 0 };
    

    This will initialise the entire array to zero (objects in C are never partially initialised).

    Alternatively, you could set just the first character to zero, since that will also make strcat do the right thing:

    char result[10000];
    result[0] = '\0';
    

    (Also, you should be passing the buffer size to lookup_hostname and using strncat / snprintf instead of strcat / sprintf).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Found the solution... I needed to use the Text property,… May 14, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer Although I'd personally use repository methods, you can do this… May 14, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer Try dateutil, which has a tzlocal type that does what… May 14, 2026 at 7:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.