Re: FastCGI binding or implementation?

2011-10-19 Thread Adam Ruppe
Andrea Fontana:
 other http methods have nothing special

Indeed. The only thing that might get you is if the data's
content type is different than the default.

That's possible on POST too, though, so still nothing special.

My cgi library has an enum to tell you what the requestMethod is,
and it lists all the options in the standard.

It, however, does not handle all possible content-types. It does
x-www-form-urlencoded and multipart/form-data, so it can handle
virtually all web forms out there - including file uploads - but
if you want others, it'll take a minor modification. The best
way to do it is probably to not attempt to parse it in the library
at all, and just pass a range of raw data to the application.


Re: xml Bible for conversion for D

2011-10-19 Thread Adam Ruppe
I found std.xml useless too, so I wrote my own lib.

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff

Grab dom.d from there. First, convert your file to UTF-8. My lib
might work for you, but it assumes utf-8 so it will throw if it actually
encounters a non-ascii character from the 8859-1 charset.


Anyway the D code for dealing with that would look something like this:


import std.file;
import std.stdio;
import arsd.dom;

void main() {
auto document = new Document(readText(bible.xml));

// the document is now the bible

auto books = document.getElementsByTagName(b);
foreach(book; books) {
   auto nameOfBook = b.n; // Genesis for example. All xml attributes are
available this same way

   auto chapters = book.getElementsByTagName(c);
   foreach(chapter; chapters) {
auto verses = chapter.getElementsByTagName(v);
foreach(verse; verses) {
 auto v = verse.innerText;

 // let's write out a passage
 writeln(nameOfBook,  , chapter.n, :, verse.n,  , v); //
prints Genesis 1:1 In the beginning, God created [...]
}
   }
}
}


Re: FastCGI binding or implementation?

2011-10-19 Thread Adam Ruppe
Jacob Carlborg:
 Why not just cache the generated HTML and let Apache handle it.

That sounds hard... configuring Apache to do anything beyond the most
trivial of tasks is a huge pain to me.

It is easy to call cgi.setCache(true); though.

 Then it doesn't even need to start the application if the cache is
 available.

It's better with a client side cache. If it's available, you don't
have to go to the server at all!


Server side caching is something I haven't done yet; it's never
been useful to me.


Re: links to nested structure

2011-08-24 Thread Adam Ruppe
Yeah, it should work with any level of nesting.

If toPrettyChars() doesn't work out, my parent
plan is actually more like:

auto item = this.parent;
while(item) {
 str ~= . ~ item.toChars();
 item = this.parent;
}

so it'd go any level needed.


Re: const main args?

2011-08-12 Thread Adam Ruppe
Jonathan M Davis wrote:
 const(T)[] maybe,
 but as soon as you use in, you can't use any range functions.

That is, to me, the biggest problem with range functions and
something that should be fixed.

There's no defense for it aside from being the status quo. It's just a
shortcoming of the current implementation, not a principled limitation.


Re: NaCl stable ABI

2011-08-04 Thread Adam Ruppe
MSN has done very well outside the United States.


Re: NaCl stable ABI

2011-08-03 Thread Adam Ruppe
Peter Alexander wrote:
 If D were usable in NaCl that would be a huge selling point for the
 language.

Well, let's set aside my personal things and see how this might be
done.

Based on what I've read so far, it actually sounds easy enough, codegen
wise.

It might be possible to drop gdc into their hacked up gcc backend and
have it work with minimal effort.

Or, changing dmd's backend to emit code for it might not be hard
either. It looks like it'd just have to align jumps, which should
be as simple as padding labels with some nops.


The bigger difficulty I suspect will be porting the runtime... but,
since it builds on C in a lot of places, that might be simple too.



I can't find more details on what's needed to happen to the compilers,
but I'm really thinking it won't be very difficult to make it happen.


Re: NaCl stable ABI

2011-08-01 Thread Adam Ruppe
Nick Sabalausky wrote:
 Client-side scripting that throws away that god-awful JS crap being
 utterly useless?

As far as I can tell, it doesn't actually throw away JS! I've never
actually used Google Native Client, but looking through the docs,
I can't find any way to set event handlers or otherwise interact
with the html.

You still need to use Javascript to actually enhance the web page.
(if you are writing an app that isn't a web page, for the love of
God, please don't force it into the browser.) Maybe this will change
as it matures, but that's the *first* thing I would have focused on
(and indeed did in my weekend browser, although it's a different
story I guess because mine is more like an html widget than plugin.)


Also, I can't find a Firefox plugin or any other browser support.
It looks like the Google thing is Google-only, and opt in by the
individual users at that. In other words, utterly useless on real
world websites because it won't actually work for 99% of users.

The networking api looks pathetic too, and I doubt that will change,
since web browser security is supposed to be maintained here.

They added audio and video, so it might be usable for writing bad
games, but even that is poor - they don't let you access the operating
system.



It's just not a really compelling offering, even if it were ubiquitious, which,
again it isn't. It is apparently only available
on the worst browser on the market. Then, add on my belief
that javascript is fairly rare in a good site anyway (do most your
work on the server and you have a more useful environment and it
degrades more easily), and I say: useless.


Re: NaCl stable ABI

2011-08-01 Thread Adam Ruppe
From what I can tell, it's Google's alternative to Flash; they want
to make crappy games on it.

Consider that the first thing they ported to it, again, just like
their javascript nonsense, was Quake. (I think Google loves
Javascript too much to let it go anyway.)

The API has a lot of graphics and audio stuff too which reinforces
this.


Re: Sending messages using socket

2011-07-24 Thread Adam Ruppe
Browsers speak HTTP, which is a higher level protocol than
plain sockets.

If you connect to your app with a simpler program, like nc,
you'll probably see the message.

If you want to serve web pages, consider one of these options:

a) Looking up the HTTP protocol itself. You've gotta send headers
before you send data or the browser won't understand what you are
replying to.

b) Use an existing web server, like Apache, and write your app as
a cgi program



The HTTP headers you need to send on a plain socket look like this:

=
HTTP/1.0 200 OK
Content-Length: 11
Content-Type: text/html

hello world
=

instead of just plain hello world.


Re: compile time introspection and code generation - automatically serialize to json

2011-07-21 Thread Adam Ruppe
For another implementation, find the functions toJson and toJsonValue
in my web.d

http://arsdnet.net/dcode/web.d


Bascially: foreach(member; item.tupleof) {
   static if(is(typeof(member) == something)
 json it
   else static if repeat for supported type families
}


Re: D and MySQL

2011-07-19 Thread Adam Ruppe
I wrapped the libmysql C library in D and use it in a lot
of my apps.

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff

Grab database.d and mysql.d from there.

To use it:

===
import arsd.mysql;

void main() {
   auto mysql = new MySql(localhost, username, password, database name);

   // ? based placeholders do conversion and escaping for you
   foreach(line; mysql.query(select id, name from users where id  ?, 5)) {

   // access to columns by name
  writefln(%s: %s, line[id], line[name]);
  // alternatively, you can write:
  writefln(%s: %s, line[0], line[1]);

   }
}
===


There's a lot of other stuff in there too, which I'll
write up in the next week or so... but this is the basics of it.


Re: D and MySQL

2011-07-19 Thread Adam Ruppe
Trass3r wrote:
 I guess access via opDispatch would also be nice to have?!

Use mysql.queryDataObject() for that.

foreach(line; mysql.queryDataObject(...rest is the same...) {
writeln(line.id, line.name);
//line[id]
//line[name]
}


all work. But with the DataObject, you don't have integer indexes.

You can also foreach(name, value; line) to go over the returned fields, just 
like
with an associative array.


What you gain though is write support:

line.name = something else;
line.commitChanges(); // does an update



You can also create new DataObjects:

auto obj = new DataObject(mysql, users);

obj.id = 10;
obj.name = My Name;

obj.commitChanges(); // does a select. if empty, inserts. if not, updates.



The DataObject is found in database.d - it is meant to work generically with any
database backend.


Re: tiny web server in D

2011-07-14 Thread Adam Ruppe
std.socket is too low level for serving webpages. It just provides
the means to talk on the network, but doesn't do any application
protocols like http.

I've written a little http server in D, but it uses linux system
calls instead of std.socket, so it only works on linux.

http://arsdnet.net/dcode

Check out httpd.d and netman.d in there. Also, my cgi.d can work
together with them to serve web apps through the mini web server.


But as you can see, a lot of the code is parsing and writing http.

When you go to a web site, your browser sends something like this
to the server:

GET /index.html HTTP/1.1
Host: mysite.com

Then, the server replies:

HTTP/1.1 200 OK
Content-Length: 13
Content-Type: text/plain
Connection: close

Hello, world!


Then the connection is closed and the get is complete.


Re: missing: __traits(isPublic (private, etc...)

2011-06-20 Thread Adam Ruppe
Indeed, this would be nice. I want it for my web.d thing.

But, as far as I know, there's no way to get it at all right now.

A while ago, I tried to add it to the compiler. The problem is
protection is stored with the symbol itself, not the thingy returned
by getMember - that's always considered public by the compiler.

So if you pass the actual Class.member, it's easy to see if it's
public or not, but the getMember doesn't say.

(Which is why it compiles in the first place... otherwise the private
members would give visibility errors when you tried to use it there.)


In the compiler, if there was an informational field for protection
added, the trait could be made to work, but I haven't gotten
around to trying to patch that yet and afaik no one else is interested
enough to look either.


Re: How To Dynamic Web Rendering?

2011-05-14 Thread Adam Ruppe
Alexander wrote:
  Sure, there are many pros. And one significant drawback - it
  couldn't be easily embedded into HTML code.

Two notes: 1) actually, you can. 2) You don't want to.

1) I wrote a little program called dhp.d - write D in a PHP style.

http://arsdnet.net/dhp.d

About 100 lines of D - not exactly a fancy program - but it shows
a way you could do it.

Use dhp as an interpreter for your script. You write

html here
?d
  // D code here
? // back to html


When dhp is run, it compiles the file on the fly. (This implementation
doesn't cache, but it could and should)

Then, it passes control to the newly compiled file, which runs and
outputs your stuff.


Literal html outside the ?d ? tags is passed through just like php.
This is implemented by putting quotes around it and cgi.writting it
in the generated code.


2) While you could do it, you shouldn't do it. This is bad form even
in PHP. See part of my DOM template writeup for a brief on why:
http://arsdnet.net/web.d/dom.html

Basically, it gets ugly, fast.


Re: How To Dynamic Web Rendering?

2011-05-14 Thread Adam Ruppe
   Not really a solution. It doesn't work like PHP/ASP do - the result
 cannot communicate with other pages even if you implement the caching.

Can you show me a PHP example showing what you mean? I can't think
of anything you can do in PHP that D can't do at least as well. If
by communicating with other pages you mean something like php's
include(), that's trivial (just include the file here too!)


Re: How To Dynamic Web Rendering?

2011-05-14 Thread Adam Ruppe
Yea, I'm going to start storing or at least linking some of my writings
in that new web.d directory so it will be easier to find.

For a version control system though, blargh, it always feels like
extra work to me. I very rarely use them at all. Most my basic libs
though are pretty stable now. cgi.d and dom.d are both in good shape.
(cgi.d did recently get new cache control functions. I discussed that
in a forum post here: http://www.sveit.com/forum/viewtopic.php?f=25t=3407

But, the core of it, is pretty much done to me.)

The actual web.d file though - the site/api generator - has been getting
a lot of changes these last couple weeks. It's in a state of rapid
change now since I've started using it on some more projects.


Once that part is solid, I'll write about it in that new folder and
post some links to the announce group.


Re: How To Dynamic Web Rendering?

2011-05-12 Thread Adam Ruppe
 What is the counterpart in D for replacing JSP/ASP/JRuby on Rail or
 some sort of dynamic web base development?

I use D for web work by using the standard CGI interface and sometimes
an embedded http server. It's the best thing I've ever used.

 How to also code digitally sign the DLL  that was compiled in D?

The same way you sign any other dll/exe.


Re: How to break module into multiple file.

2011-05-12 Thread Adam Ruppe
Use folders as a collection of modules.

foo/bar.d = import foo.bar;

You don't actually have to separate them into folders - the directory
layout is up to you. But this is the simplest method.

Learn more here:

http://digitalmars.com/d/2.0/module.html


Re: How To Dynamic Web Rendering?

2011-05-12 Thread Adam Ruppe
 Could you share how or show an URL that provide sample code to do
 that in D?

Check out my D api demo:

http://arsdnet.net/cgi-bin/apidemo/

Here's the source code (about 50 lines of D)
http://arsdnet.net/apidemo.d

And the HTML templates it uses:
http://arsdnet.net/apidemo-document.html
http://arsdnet.net/apidemo-javascript.html


The libraries it imports are available here

http://arsdnet.net/dcode/



That demo shows something relatively new I made to provide same
source access to HTML and Javascript that uses D's reflection to
auto-generate almost everything.


You can also do more traditional style web apps. Here's a gradient
generator written in D:

http://arsdnet.net/cgi-bin/gradient?w=100h=100c1=ffc2=00ff00c4=00t=50

Source:
http://arsdnet.net/gradient.d

It uses my cgi.d from the dcode folder above. This code has a few
features so it isn't the simplest demo, about 150 lines. It makes
the png and sends it right out over the web.


Hello world with cgi.d looks like this:

import arsd.cgi;
void main() {
   auto cgi = new Cgi();
   cgi.write(Hello, world!);
   cgi.close();
}


In all my examples though, I used mixins for main functions instead
of writing them directly because the mixin adds try/catch for
exceptions and handles the create and close too. This would look like:

import arsd.cgi;
void run() { cgi.write(Hello, world!); }
mixin GenericMain!run;


I also often use my D DOM library in web code too, which you see
in the first example. That library is available in my folder too.

 So we use external tool to do that and not a build in one??

Yes, something like signtool.exe from Microsoft works for Windows
programs.

Generally, if you can do it to a C++ program, you can do it to a
D program too.


Re: How to break module into multiple file.

2011-05-12 Thread Adam Ruppe
 so I couldn't have different namespaces (on different levels) in
 single directory, and this also forces me to match file
 name with module name.

No, it doesn't. You're right that it splits when it searches, but it
doesn't *have* to search.

If you use the module declaration at the top of the file and manually
list the files on the command line, the file and directory names don't
matter.


a.d

import cool.wtf;
void main() {}


b.d

module cool.wtf;


dmd a.d b.d # works!


Re: How to break module into multiple file.

2011-05-12 Thread Adam Ruppe
 For small projects this is OK, but for large - not quite, IMHO.

I don't know about that. I always compile everything at once
now - in my experience, it's negligibly slower than linking alone.

druntime: compiled all at once takes ~1 second on my box. 65k lines of (light) 
code.

phobos: compiled all at once takes ~1 second. 150k loc.

my web app for work: 3 seconds, all at once. 50k loc, does some crazy stuff.


Maybe million line programs will be unacceptably slow, but I don't
know, I'd have to actually see it being a problem in practice
before I get worked up about it. tbh I wouldn't be surprised if
the incremental build was actually slower than the all at once in
a lot of situations.

(Though, naming the packages and modules to match the filesystem is
a small cost anyway - it makes it easier to find the files so usually
the best choice anyway.)


Re: How To Dynamic Web Rendering?

2011-05-12 Thread Adam Ruppe
// Read in HTTP request headers

Actually, that's not quite right. The HTTP headers are sent
as environment variables. stdin gives you the POST variables -
with the encoding given in the HTTP_CONTENT_TYPE envvar iirc.
(that's where the Content-Type http header is found).

The most common type is urlencoded, so it's just like parsing the
query string. The other one I handle is multipart - the encoding for
file upload forms.

Getting all that stuff out is the main benefit of a basic cgi library..
it's a bit of a pain to do yourself every time.


Re: Calling C functions

2010-12-09 Thread Adam Ruppe
I almost forgot I wrote that dtips page! Thanks for bringing it up.

I just updated it to include a brief note on __gshared. The rest of it should
still work, though. Calling C functions is pretty easy.


How to exit a process?

2010-12-06 Thread Adam Ruppe
Is there a thing like C's exit() for a D program? Is
simply using C's function good enough?