This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TehSomeLuigi's profile picture

[Cc 1.56] [Mc 1.6.x] Someluigi's Peripheral(S) 2 - Welcome Back, Http Server

Started by TehSomeLuigi, 05 March 2013 - 09:11 AM
TehSomeLuigi #1
Posted 05 March 2013 - 10:11 AM
SomeLuigi's Peripherals 2

Hi, this is my little peripherals mod. I made it for 1.5.1, when I was still quite new to modding. I added a port of Portable Peripherals not long after that, and the code got messy to maintain. Now I rewrote it for 1.6.2, however, the Portable Peripherals port is not here anymore.

The new version was made for 1.6.2, but it also works on 1.6.4 (for me, at least).

Without further ado,

Peripheral(s)

HTTP Server Module,

^ shown above is the first texture of v1. It's changed now.

Usage
SpoilerWrap the peripheral as usual.

Call (where p is your wrap) p.start() to start the service, and p.stop() to stop it.

While the service is running, your computer can receive an event - "http_server_request".

To trigger this event, someone must open a web browser (or other Internet-accessing thing) to the webserver.

By default, the webserver runs on port 8080. This may require port forwarding if you host a server.

And the address should be whatever server you are playing on.

Single player possibilities:
localhost:8080
127.0.0.1:8080
0.0.0.0:8080

Server (using random names):
mcserver.eu => mcserver.eu:8080
playmc.com:26943 => playmc.com:8080

I hope this makes sense to you, I'm still at a loss of how to explain that. Anyway, pop the new address into your browser and you should see a page asking you to enter a Computer/Turtle's ID.

If my private server is currently running, you can see an example here: http://sl.teamredscript.com:8080/

Once you do so, and hit the button, it will change the URL to add that ID. It pretty much just adds the id to the URL, like http://sl.teamredscript.com:8080/5/

Okay, now back to the events.
If I access the URL http://sl.teamredscript.com:8080/5/ ,
computer 5 would receive an event:

Name: http_server_request
RequestID: 0
URL: /
Query Table (Lua Table): {}
Cookie Table (Lua Table): {}

This is when you should process the request. We will simply write back a page informing the user what URL they accessed, and set a cookie containing this information (for whatever reason)

p.setCookie(requestID, cookieName, cookieValue)
will set a cookie in the browser of name and value provided. Use the request ID you receive from the event.

p.respond(requestID, responseText, contentType*)
* optional
Use the request ID you receive from the event. ResponseText is what is wrote back to the browser.
Content type is the mime-type of your response. It defaults to "text/html" (HTML). If you want to show as text, you can use "text/plain".


If you need any clarification with this please ask ^^.




Config option configures whether or not the webserver runs, and which port, plus the block Id. The webserver only starts on the ServerStarting event, so not at the main menu, but only while in a world. The webserver does NOT exist on the client when connected to a MP server.



Oh, I nearly forgot (like the idiot I am ^^), Download:
Latest Version 2.0-3 for 1.6.x
All Versions, plus Links to each version's github (SOURCE CODE) tag
GitHub





version 1: Old Post
SpoilerAttention: I will be porting this over the next few days to 1.6.2. It's about time, don't you think? :)/>
At that time I will clean up the code a lot, and seperate PortablePeripherals again from my code. I suspect if/when PDAs are implemented in vanilla CC it won't be necessary, then I will port it again to the latest CCDesk if that's needed, but no promises.

~ TSL, 24 August 2013 at 13:20 GMT+1

Old Post
SpoilerHello everyone, this is my first PUBLIC ComputerCraft mod here. I plan to extend it, though I don't know what with.
If you have good ideas, feel free to suggest them, I'll try and put some in. Anyways, on with the mod.


Have you ever messed with the HTTP api, maybe so that you can put a web interface on your world? I have, but I thought it would work out better if ComputerCraft was the webserver (and not a client constantly checking somewhere else for commands).

I made it.

This peripheral starts a webserver ( powered by Simple Framework, http://www.simpleframework.org/ ) that can interact with your computers.

Crafting:


If picture does not show:

[ REDSTONE ] [ REDSTONE TORCH ] [ REDSTONE ]
[ GOLD INGOT ] [ IRON BLOCK ] [ GOLD INGOT ]
[ GOLD INGOT ] [ IRON INGOT ] [ GOLD INGOT]

makes

1 HTTP Server Service Module


USAGE:
SpoilerOkay, I lied. The peripheral doesn't really start a webserver, that'd be too many webservers. I was smarter than that. When your game starts, a webserver is started. Only one.

When you start up the peripheral, the computer is given a subdirectory to reside in. It never sees outside. This is called a "service"

So, yes.

Wrap the "HTTP Server Service Module" (crafted above) like you would a normal peripheral.

Use the "start" method to start the service.
Use the "stop" method to stop the service.

Next: dealing with HTTP Requests

So, if you have computer ID 5, your URL will be at:
http://localhost/5/a_page

Please note that localhost should be the IP (or localhost if you play in SP), and if you change the port add : then the port number.

When an event is sent to your computers service, the computer receives an event called "http_server_request". Pay attention to the underscores.

The event also has 3 parameters.

Request Id - You will need to give this ID back when you respond to the request.
URL - This will be something like /a_url if http://localhost/5/a_url was visited.
Query - This is a table of the GET and POST variables. This means you can use forms.
It can be accessed like query.aValue for the aValue field, also like query["aValue"]… A Normal Lua Table.

Now you have to respond. To do this, use the "respond" method of the peripheral. You must use it like so:

http.respond(Request ID, HTML/Content to go Back)

If you want to use images you will have to link them from a website, I think.



If you notice at http://localhost/ there is a default message. Pretty simple.


If you are playing on a server with it installed, the IP address is most likely the same as the Minecraft Server.






Here is an example program that takes the peripheral on top of the computer, starts the service then waits for 1 request. When it gets this, it will show the request ID, the URL, and the Query vars in the computer console. It will also send back 'You are at URL: (url)' to the browser.


http = peripheral.wrap('top')
http.start()
id, req, url, query = os.pullEvent('http_server_request')
print('Request ID: ' .. req)
print('Accessing URL: ' .. url)
for k,v in pairs(query) do
print('Query Var ' .. k .. ' = ' .. v)
end
resp='You are at URL:
'
resp=resp..url
http.respond(req, resp)
http.stop()


Configuration (CONFIG):
SpoilerYou can edit the block ID of the peripheral.

You can disable crafting of the HTTP Server Service Module.

You can enable Debug Mode. This is pretty much for me when I couldn't figure out why the peripherals weren't connecting.

You can enable/disable the HTTP Server.

You can also change the port of the HTTP Server. This could be useful if you already have a server on port 80.

Different style of documentation
SpoilerPeripheral Name: http-server
Methods:
start() // starts SERVICE
stop() // stops SERVICE
respond(Request_ID, ContentBody) // headers are sorted for you, and untouchable. - until further notice


Events (only when SERVICE is started)
http_server_request:
Request Identifier (pass to respond)
URL (http://server-ip/computer-id/aUrl => /aUrl)
Query table (GET and POST variables, preprocessed courtesy of Simple Framework)


More about accessing your webserver:
SpoilerI've been mentioning localhost all the way through. This is your server IP. On single player, you can use 127.0.0.1 or localhost as this is your server's IP. If you use a port other than 80 (in config file) you WILL NEED TO PUT A colon ( :)/> then the port number after the IP/address. Example: localhost:8080

If your server is on a domain, that works too (play.serversite.com), and same port rules apply.

Hello everyone, here is my peripheral mod. Before you start reading the post, I'll shove credits at the top:
Credits
mentlerd - creating the original PortablePeripherals (the mod was discontinued so I ported it into my mod)
Simple Framework - http://www.simpleframework.org/ (this is the engine used for the HTTP Server)


Peripherals
HTTP Server Service Module

peripheral.getType( side ) = http-server

This allows you to host a "service" on HTTP.
By visiting http://SERVER ADDRESS:HTTP PORT/COMPUTER ID/ (i.e. http://play.a-mc-server.com:8080/5/),
you can see the service. By using the chat command /slp, you can see the status of the webserver. If it's enabled it'll tell you the port. The port number can be changed in the config.
Warning: Linux users (like me) will need to either use a port number above 2048 or run the game as root. As a word of advice, DO NOT run the game as root, EVER. So just use a port number like 8080. If you use a port which you don't have permission to open, typing /slp will yield:

Oh, and if you're playing singleplayer, the address will be 127.0.0.1.
And if you're trying to access the server from the computers ingame (i.e. the http api), it's also 127.0.0.1

http-server:
methods
isActive() - returns whether the HTTP Server is enabled in the config
start() - Starts servicing
stop() - Stops servicing
respond(request_id, response_text) - responds to REQUEST_ID with RESPONSE_TEXT as HTML

events
http_server_request:
Request Identifier (pass to respond)
URL (http://server-ip/computer-id/aUrl => /aUrl)
Query table (GET and POST variables, preprocessed courtesy of Simple Framework)

Cookie Table (as of 1.3)
example: e, request_id, url, query, cookie = os.pullEvent('http_server_request')



Please see Portable Peripheral's origin post until I add the information here.
http://www.computerc...le-transmitter/
But watch out, the recipe changed to require Nether Quartz:







Changelog:

v1.3 (changes since 1.2)
- Relocated textures
- Cleaned up source code
- Added a port of PortablePeripherals (discontinued) by mentlerd to the mod. The quartz are now crafted like:
G
Q
G
where: G=Glass, Q=Nether Quartz
- Fixed Wireless Transmitter (PP) not persisting.
- Added cookies table parameter to http_server_request event
- You can find the demo files in the computer's slperiph/ directory after you mounted one of my peripherals. Currently there is mentlerd's transmit program for the Wireless Transmitter (PP).

Spoilerv1.3pre1
- Relocated textures
- Cleaned up source code
- Added a port of PortablePeripherals (discontinued) by mentlerd to the mod. The quartz are now crafted like:
G
Q
G
where: G=Glass, Q=Nether Quartz

!!! Major Bug: In this prerelease, items placed in the Transmitter block will be lost when you exit the world or unload the chunk. Will be fixed tomorrow.

v1.2
- Port to Minecraft 1.5.1
- Now uses Searge (SRG) names and takes advantage of runtime deobfuscation, it should be version-independant now, so it'll work on 1.5 even though it's built for 1.5.1.
- The top and bottom textures are now the heat vents I've had in the textures for a while. Servers do get hot sometimes.


v1.0b

- Now starts on ServerStarting Events (when you load an SP world or start an SMP server)
- Now stops on ServerStopping Events (so when you exit your SP world it's not still running).
- Updated textures to those provided by TheModerGuy


v1.0

- Initial Release.

Cool things:
SpoilerTurtle Remote Control by Android (screenshot: see post)




Anyways, have fun and if you'd like to see something in this please leave a suggestion.

Also leave feedback, bug reports, and such.

Oh, and of course, the download:


ComputerCraft Edition

v1.3: http://bit.ly/Yk4P4c (bit.ly for stats)

All Versions (Old + Prereleases): https://www.dropbox....kj/2Iw-U9YdXU?m

Source on github: https://github.com/T...is-peripherals/


CCDesk (Emulator) Edition

Please check the complete/missing list below to see the peripherals which are missing.

v1.3-a / Port of 1.3 revision A: http://bit.ly/179aygR (bit.ly for stats)

All Versions (Old + Prereleases): https://www.dropbox....6mq0/ZotIYgiADI

Source on github: https://github.com/T...lperiph-deskcc/

Complete/Missing:
- PortablePeripherals: Everything is missing
- SLP HTTP Server Service: Complete (v1.3, in v1.3-a)

If you make something cool with it, share it, I'm interested to see what you can do with it. I might add your creation to the post (with credit of course) and maybe ship it with the mod (if I start shipping demo files, that is).
Shnupbups #2
Posted 05 March 2013 - 10:15 AM
OMFG! Awesome! NEED to add this to my GIANT personal mod-pack…

EDIT: My 66th mod on it!
EDIT2: If you need some textures, I kinda suck but I can try!
Simon #3
Posted 05 March 2013 - 01:26 PM
Nice

Edit: Works perfectly :D/>
electrodude512 #4
Posted 05 March 2013 - 01:46 PM
Can I use this on my private whitelist server?

Thanks,
electrodude
TehSomeLuigi #5
Posted 05 March 2013 - 09:01 PM
Thanks for the feedback!

And yes, feel free to put it on your server or modpack.

Let me know how it goes!
superaxander #6
Posted 06 March 2013 - 09:00 AM
This is awesome!!
TehSomeLuigi #7
Posted 06 March 2013 - 11:49 AM
Thanks.

I shall be adding cookie and header support soon. Probably also a way to host your paint/npaintpro as pngs.
TheModerGuy #8
Posted 06 March 2013 - 09:15 PM
i made textures for the mod, they make the module look more "real" as in look like servers in real life, GREAT MOD I LOVE IT!
Android #9
Posted 09 March 2013 - 06:43 AM
Cool stuff! Thanks!

Based on this HTTP-Server mod I programmed a turtle remote control for Android smartphones. Anyone interested in?

SpoilerScreenshot
Simon #10
Posted 09 March 2013 - 07:02 AM
Cool stuff! Thanks!

Based on this HTTP-Server mod I programmed a turtle remote control for Android smartphones. Anyone interested in?

SpoilerScreenshot

Wow, I might just have to buy an android for that.
Android #11
Posted 10 March 2013 - 05:14 AM
Hi TehsomeLuigi

Thanks again for the awesome work you did! :)/>
I realised that the MC-Client still provides the HTTP-service when you play multiplayer on a foreign server.
The computer ID seems not to work then though. When I a make a request to the ip-address of the client I get an answer from the SLP HTTP Server that the desired service is not online.

Is there a way to correctly address the computer-ID on the HTTP server on the client's side?
immibis #12
Posted 10 March 2013 - 04:08 PM
Hi TehsomeLuigi

Thanks again for the awesome work you did! :)/>/>
I realised that the MC-Client still provides the HTTP-service when you play multiplayer on a foreign server.
The computer ID seems not to work then though. When I a make a request to the ip-address of the client I get an answer from the SLP HTTP Server that the desired service is not online.

Is there a way to correctly address the computer-ID on the HTTP server on the client's side?
If there's a client HTTP server at all, that's a bug.
Android #13
Posted 11 March 2013 - 02:28 AM
Hi TehsomeLuigi

Thanks again for the awesome work you did! :)/>/>
I realised that the MC-Client still provides the HTTP-service when you play multiplayer on a foreign server.
The computer ID seems not to work then though. When I a make a request to the ip-address of the client I get an answer from the SLP HTTP Server that the desired service is not online.

Is there a way to correctly address the computer-ID on the HTTP server on the client's side?
If there's a client HTTP server at all, that's a bug.

I suspected that this was not intended in the first place. But I wonder about whether one can turn this "bug" into a "feature"? :rolleyes:/>
TehSomeLuigi #14
Posted 11 March 2013 - 07:06 AM
Yep, the client HTTP is a bug. I could make it redirect to the server's IP but to be fair you'd be better off using the server's IP.

I'll fix the bug soon. Maybe more features to come, will also use the textures sent.

Your Turtle RC looks cool :)/>.
Android #15
Posted 11 March 2013 - 07:50 AM
Yep, the client HTTP is a bug. I could make it redirect to the server's IP but to be fair you'd be better off using the server's IP.

I'll fix the bug soon. Maybe more features to come, will also use the textures sent.

Your Turtle RC looks cool :)/>.
Thanks!
I see.. :(/> Maybe you could allow the redirection as an option? This would certainly make it a lot easier for many users. :rolleyes:/>
Anyway… I am perfectly satisfied with the present and I am looking forward to more features to come. :D/>
TehSomeLuigi #16
Posted 11 March 2013 - 09:40 AM
I updated to 1.0b. Just the bugfix and texture change:

- Now starts on ServerStarting Events (when you load an SP world or start an SMP server)
- Now stops on ServerStopping Events (so when you exit your SP world it's not still running).
- Updated textures to those provided by TheModerGuy


+ OP Change: Added Android's Turtle Remote Control

Cool stuff! Thanks!

Based on this HTTP-Server mod I programmed a turtle remote control for Android smartphones. Anyone interested in?

SpoilerScreenshot

I added a link to your post in the OP.





i made textures for the mod, they make the module look more "real" as in look like servers in real life, GREAT MOD I LOVE IT!
Spoiler

Thanks, your textures are now what is used as of v1.0b.




What would people like to see next?
I was thinking something that might supplement the HTTP Server well is an image encoder. Because right now there's a bit of a lack of images in the servers (it'd make a PNG. Maybe animated gifs would be possible, too). Then I'd also add a method to the HTTP Server to transfer the file by filename (so you can send files that aren't plaintext without them breaking).

If you have an idea, feel free to share!
Android #17
Posted 13 March 2013 - 06:23 AM
What would people like to see next?
I was thinking something that might supplement the HTTP Server well is an image encoder. Because right now there's a bit of a lack of images in the servers (it'd make a PNG. Maybe animated gifs would be possible, too). Then I'd also add a method to the HTTP Server to transfer the file by filename (so you can send files that aren't plaintext without them breaking).

If you have an idea, feel free to share!
Yeah… images and file transfer would be awesome! :D/> And of course an optional redirection to the server-IP would be great ;)/> Not every occasional/laymain server-admin is willing (or able) to provide the otherwise required port-forwarding. (But, don't let you push you.. ;)/> )
immibis #18
Posted 13 March 2013 - 10:00 AM
How are people playing on the server if you don't have port forwarding?
Android #19
Posted 13 March 2013 - 10:28 AM
I had to experience that admins self-explanatory forward the default 25565 MC port but are reluctant to forward any other port sometimes. Apparently for "security" reasons or because they simply don't go the extra mile.
immibis #20
Posted 13 March 2013 - 07:54 PM
If your admin forwards 25565, installs the HTTP Server peripheral and doesn't forward that port, your admin is stupid, and you should take it back to the manufacturer for a replacement.
Android #21
Posted 13 March 2013 - 09:15 PM
If your admin forwards 25565, installs the HTTP Server peripheral and doesn't forward that port, your admin is stupid, and you should take it back to the manufacturer for a replacement.
Lol.. I agree
Android #22
Posted 14 March 2013 - 01:33 AM
What would people like to see next?
I was thinking something that might supplement the HTTP Server well is an image encoder. Because right now there's a bit of a lack of images in the servers (it'd make a PNG. Maybe animated gifs would be possible, too). Then I'd also add a method to the HTTP Server to transfer the file by filename (so you can send files that aren't plaintext without them breaking).

If you have an idea, feel free to share!

You were asking for ideas… well, how about a Webcam-Block, which you could connect to your HTTP Server and which would provide still pictures?
Viproz #23
Posted 18 March 2013 - 04:45 AM
You were asking for ideas… well, how about a Webcam-Block, which you could connect to your HTTP Server and which would provide still pictures?

Do you imagine the traffic it will use to upload a constant video ?
Android #24
Posted 18 March 2013 - 05:47 AM
You were asking for ideas… well, how about a Webcam-Block, which you could connect to your HTTP Server and which would provide still pictures?

Do you imagine the traffic it will use to upload a constant video ?
I was talking about still pictures.
TehSomeLuigi #25
Posted 03 April 2013 - 05:43 AM
If your admin forwards 25565, installs the HTTP Server peripheral and doesn't forward that port, your admin is stupid, and you should take it back to the manufacturer for a replacement.

Haha, take the server admin back to the manufacturer? Not a bad idea. :)/>






TehSomeLuigi said:
*snip*

You were asking for ideas… well, how about a Webcam-Block, which you could connect to your HTTP Server and which would provide still pictures?

It would provide pictures of the ingame world? I don't know how to do that, sorry, I don't think the server has any type of rendering either.




Anyways,
sorry for my absence, I'll update for 1.5.1 now.
Simon #26
Posted 10 April 2013 - 07:01 AM
This MUST be added to CC-Desk. It will be epic!
superaxander #27
Posted 10 April 2013 - 07:21 PM
This MUST be added to CC-Desk. It will be epic!
Indeed it will
TehSomeLuigi #28
Posted 13 April 2013 - 07:54 AM
This MUST be added to CC-Desk. It will be epic!

I've tried, but I can't get the plugin system to work. I've opened an issue and maybe it'll get resolved and then I'll be able to do it (hopefully).
TehSomeLuigi #29
Posted 13 April 2013 - 01:37 PM
https://www.dropbox.com/sh/kkq3o2gw84p11kj/2Iw-U9YdXU?m

You can find 1.3pre1 at the above. Please read the changelog for info and major bug warning. The link will be added to the OP once I can get on my PC again.
TehSomeLuigi #30
Posted 14 April 2013 - 01:25 AM
Okay, 1.3 stable is released. I fixed the persist problem (silly me, forgot to add the tileentity mapping).

Have fun, and report bugs! (and give suggestions!)

My suggestion, what do you think:

Ender Terminals/PDAs, (like the wireless one, but cross-dimension + unlimited range)
You'd have to charge it at an Ender Charge Station where you can refuel it with Liquid Ender (if available, from Thermal Expansion) or by smelting Ender Balls.

It'd use some Ender Charge when you use it. Just an idea, what do you think of it?

I'm also accepting other ideas.
PixelToast #31
Posted 01 May 2013 - 10:55 AM
any specific reason your spamming?

2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
why are you checking if i opened a wireless terminal 20 times a second anyway?
TehSomeLuigi #32
Posted 17 May 2013 - 03:58 PM
any specific reason your spamming?

2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
2013-05-01 10:53:09 [INFO] [STDOUT] No Display
why are you checking if i opened a wireless terminal 20 times a second anyway?

Firstly, the PortablePeripherals is just a port. I don't really know the full way it works.

But that being said, I can't see anywhere where such an information might be produced.

I also don't get this error myself. What are you doing when this happens? I understand you don't like it when it spams, I don't either, but I can't see the cause of this problem.
TehSomeLuigi #33
Posted 26 July 2013 - 07:07 AM
Next version I will probably be seperating PortablePeripherals since CC will be adding its own PDA stuff soon. As in, seperate into another mod.

New features will probably be none. But a port to 1.6.2.
Thib0704 #34
Posted 29 July 2013 - 09:15 AM
This look's awsome, Just waiting For 1.6.2 Now.
bigibas123 #35
Posted 16 August 2013 - 08:34 AM
asome!
TehSomeLuigi #36
Posted 30 September 2013 - 04:15 PM
Release of version 2 for 1.6.x.

Have fun!
NYLoRD #37
Posted 15 June 2014 - 06:22 PM
can someone give me a code example ?

when I run the url localhost: 8080/41 it loads and does not stop.
chrdov #38
Posted 16 June 2014 - 12:09 AM
please update it to work with 1.6x. I want to use it with the latest version, 1.63

thanks.