---- Daniel Gruno <[email protected]> wrote:
> On 06/20/2012 05:21 PM, [email protected] wrote:
> >
> > ---- Ben Noordhuis <[email protected]> wrote:
> >> On Wed, Jun 20, 2012 at 4:35 PM, <[email protected]> wrote:
> >>> Hi,
> >>>
> >>> I am working on a module, and I get one of the SSL envvars,
> >>> SSL_CLIENT_CERT, using apr_table_get() into a const char *.
> >>>
> >>> The client cert char string returned has the extra beginning line
> >>> (-----BEGIN CERTIFICATE-----) and ending line (-----END
> >>> CERTIFICATE-----), but I need to remove both of those lines for a call
> >>> that I need to make.
> >>>
> >>> I have to admit, I'm a bit (a lot) rusty with 'C', and I guess I could do
> >>> something like:
> >>>
> >>> strpy(original_cert, original_cert+27);
> >>>
> >>> and then set the ending position to \0 (to terminate the char string
> >>> early), but since with this is a module, and I'm working with a pointer
> >>> to the memory pool, I'm kind of worried that doing stuff like that would
> >>> mess things up (e.g., garbage collection, since the string is now shorter
> >>> by 'x' bytes.
> >>>
> >>> So, from an Apache module development standpoint, what would be the
> >>> safest way to do this (strip a string of chars from the beginning and
> >>> end)?
> >>
> >> Make a copy with apr_strdup(), then mutate the copy.
> >>
> >> APR has utility functions for manipulating strings, like apr_strtok().
> >> Have a look at apr_strings.h.
> >
> > Hi,
> >
> > Thanks. I've been using those, and I can eliminate the beginning line, but
> > how can I eliminate the ending line? It seems that I can't just store a
> > '\0' into the end of the char string.
> >
> > Will apr_cpystrn() automatically terminate the destination char string,
> > i.e., if I do something like:
> >
> > apr_cpystrn(cert_without_ending, cert_string, strlen(cert_string)-10);
> >
> > will the cert_without_ending char string get terminated properly with \0?
> >
> > Jim
> Yes, apr_cpystrn includes a terminating null char, as mentioned in the
> documentation;
> http://apr.apache.org/docs/apr/1.4/group__apr__strings.html#ga69700a825e82dd646f9f166599040431
>
> With regards,
> Daniel.
Hi Ben and Daniel,
I must be doing wrong. After I added code to strip the begin and end lines, I
get a segfault when I test.
I notice that the apr_cpystrn() doesn't include a pool parameter, so I guess
it's just moving the char strings in memory that already exists in the pool?
Here's the code I have thus far:
const char * ssl_client_cert;
const char * ssl_client_cert_after_remove_begin;
const char * ssl_client_cert_after_remove_end;
int end_of_cert;
printf("SSL_CLIENT_CERT=[%s]\n", apr_table_get(r->subprocess_env,
"SSL_CLIENT_CERT"));
ssl_client_cert = apr_table_get(r->subprocess_env, "SSL_CLIENT_CERT");
printf("printf ssl_client_cert=[%s]\n", ssl_client_cert);
printf("ssl_client_cert + 27=[%s]\n", ssl_client_cert+27);
// SEGFAULT after here...
end_of_cert = strlen(ssl_client_cert);
apr_cpystrn(ssl_client_cert_after_remove_begin, ssl_client_cert+27,
end_of_cert-27 );
end_of_cert = strlen(ssl_client_cert);
printf("printf ssl_client_cert_after_remove_begin=[%d]-[%s]\n",
end_of_cert, ssl_client_cert_after_remove_begin);
end_of_cert = strlen(ssl_client_cert_after_remove_begin);
apr_cpystrn(ssl_client_cert_after_remove_end,
ssl_client_cert_after_remove_begin+21, end_of_cert-21);
printf("printf ssl_client_cert_after_remove_end=[%d]-[%s]\n", end_of_cert,
ssl_client_cert_after_remove_end);
Thanks,
Jim