Browse files

LWP/Protocol/ldap.pm: add JSON output support

  • Loading branch information...
1 parent 6c1fecf commit 390e49cdffedbed90a2f02b904455f344a813291 @marschap marschap committed Sep 6, 2012
Showing with 24 additions and 1 deletion.
  1. +24 −1 lib/LWP/Protocol/ldap.pm
View
25 lib/LWP/Protocol/ldap.pm
@@ -13,7 +13,7 @@ use LWP::MediaTypes ();
require LWP::Protocol;
@ISA = qw(LWP::Protocol);
-$VERSION = "1.17";
+$VERSION = "1.18";
use strict;
eval {
@@ -69,6 +69,7 @@ sub request {
# analyse HTTP headers
if (my $accept = $request->header('Accept')) {
$format = 'ldif' if $accept =~ m!\btext/(x-)?ldif\b!;
+ $format = 'json' if $accept =~ m!\b(?:text|application)/json\b!;
}
if (!$user) {
@@ -152,6 +153,28 @@ sub request {
$response = $self->collect_once($arg, $response, $content)
if ($method ne 'HEAD');
}
+ elsif ($format eq 'json') {
+ require JSON;
+
+ my $entry;
+ my $index;
+ my %objects;
+
+ for ($index = 0 ; $entry = $mesg->entry($index); $index++) {
+ my $dn = $entry->dn;
+
+ $objects{$dn} = {};
+ foreach my $attr (sort($entry->attributes)) {
+ $objects{$dn}{$attr} = $entry->get_value($attr, asref => 1);
+ }
+ }
+
+ my $content = JSON::to_json(\%objects, {pretty => 1, utf8 => 1});
+ $response->header('Content-Type' => 'text/json; charset=utf-8');
+ $response->header('Content-Length', length($content));
+ $response = $self->collect_once($arg, $response, $content)
+ if ($method ne 'HEAD');
+ }
else {
my $content = "<head><title>Directory Search Results</title></head>\n<body>";
my $entry;

0 comments on commit 390e49c

Please sign in to comment.